Added solution to problem 6
This commit is contained in:
BIN
sumSquareDifference/main
Executable file
BIN
sumSquareDifference/main
Executable file
Binary file not shown.
7
sumSquareDifference/main.c
Normal file
7
sumSquareDifference/main.c
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int func();
|
||||
|
||||
int main() {
|
||||
printf("%d\n",func());
|
||||
}
|
@@ -2,13 +2,42 @@
|
||||
.section .text
|
||||
|
||||
func: @ Find the difference between the sum of the squares and the square of the sum, for the first hundred numbers.
|
||||
@ (a^2+b^2) = a^2 + b^2 + 2ab
|
||||
@ Therefore, the difference must be 2ab
|
||||
|
||||
|
||||
|
||||
@ (a+b)^2 = a^2 + b^2 + 2ab
|
||||
@ (a+b+c)^2 = a^2 + b^2 + c^2 + 2(ab +bc + ca)
|
||||
@ Therefore, the difference must be 2(ab + ac + ad .... + bc + bd + be ... + cd + ce .....)
|
||||
|
||||
MOV R0,#0
|
||||
MOV R2,#1 @ 'a' from the equation above
|
||||
MOV R3,#2 @ 'b' from the equation above
|
||||
|
||||
mainloop:
|
||||
MUL R1,R2,R3 @ 'a' * 'b' - keep in mind we need to do this for all possible 2-number permutations in the sequence
|
||||
ADD R0,R0,R1 @Add the result to R0, which will be returned
|
||||
|
||||
incr_b:
|
||||
ADD R3,R3,#1 @ Increments 'b'
|
||||
|
||||
chk_b:
|
||||
CMP R3,#100 @ If 'b' is less than or equal to 100, we continue the multiplication
|
||||
BLE mainloop
|
||||
|
||||
incr_a:
|
||||
ADD R2,R2,#1 @ Increments 'a'
|
||||
|
||||
new_b: @This is a way for me to store R2+1 into R3
|
||||
ADD R2,R2,#1
|
||||
MOV R3,R2
|
||||
SUB R2,R2,#1
|
||||
|
||||
chk_a:
|
||||
CMP R2,#100
|
||||
BLT mainloop
|
||||
|
||||
|
||||
mul2:
|
||||
LSL R0,R0,#1 @Bitwise left shit by 1 - Multiplies by 2
|
||||
|
||||
return:
|
||||
BX LR
|
||||
|
||||
.section .data
|
||||
|
Reference in New Issue
Block a user