Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.
The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)² = 55² = 3025.
The sum of the squares of the first ten natural numbers is 1² + 2² + ... + 10² = 385.
Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.
Problem 6 at Project Euler http://projecteuler.net/problem=6
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
# # Test difference_of_squares with some examples # # s0 - num of tests left to run # s1 - address of input word # s2 - address of expected output word # s3 - input word # s4 - output word # # difference_of_squares must: # - be named difference_of_squares and declared as global # - read input integer from a0 # - follow the convention of using the t0-9 registers for temporary storage # - (if it wants to use s0-7 then it is responsible for pushing existing values to the stack then popping them back off before returning) # - write integer result to v0 .data n: .word 4 # number of test cases ins: .word 0, 5, 10, 100 # input numbers outs: .word 0, 170, 2640, 25164150 # expected result failmsg: .asciiz "failed for test input: " expectedmsg: .asciiz ". expected " tobemsg: .asciiz " to be " okmsg: .asciiz "all tests passed" .text runner: lw $s0, n la $s1, ins la $s2, outs run_test: lw $s3, 0($s1) # read input from memory move $a0, $s3 # move it to a0 jal difference_of_squares # call subroutine under test move $v1, $v0 # move return value in v0 to v1 because we need v0 for syscall lw $s4, 0($s2) # read expected output from memory bne $v1, $s4, exit_fail # if expected doesn't match actual, jump to fail addi $s1, $s1, 4 # move to next word in input addi $s2, $s2, 4 # move to next word in output sub $s0, $s0, 1 # decrement num of tests left to run bgt $s0, $zero, run_test # if more than zero tests to run, jump to run_test exit_ok: la $a0, okmsg # put address of okmsg into a0 li $v0, 4 # 4 is print string syscall li $v0, 10 # 10 is exit with zero status (clean exit) syscall exit_fail: la $a0, failmsg # put address of failmsg into a0 li $v0, 4 # 4 is print string syscall move $a0, $s3 # set arg of syscall to input that failed the test li $v0, 1 # 1 is print int syscall la $a0, expectedmsg li $v0, 4 syscall move $a0, $v1 # print actual that failed on li $v0, 1 syscall la $a0, tobemsg li $v0, 4 syscall move $a0, $s4 # print expected value that failed on li $v0, 1 syscall li $a0, 1 # set exit code to 1 li $v0, 17 # terminate with the exit code in $a0 syscall # # Include your implementation here if you wish to run this from the MARS GUI. # .include "impl.mips"
.globl difference_of_squares # $t0 - square of sums # $t1 - sum of squares # $t2 - a square # registers that are referenced before assignment need to be cleared .text difference_of_squares: move $t0, $zero move $t1, $zero LOOP: beqz $a0, STOP mul $t2, $a0, $a0 add $t1, $t1, $t2 add $t0, $t0, $a0 subi $a0, $a0, 1 j LOOP STOP: mul $t0, $t0, $t0 sub $v0, $t0, $t1 jr $ra
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.