11 Apr, 2016 @ 23:23

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

This one was simple, easy and quick. First we start with 20, then increment in intervals of 20. Since 20 is the top number, we just keep incrementing by 20 until we find a number divisible by everything

# https://projecteuler.net/problem=5 # 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. # # What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? # # Solution by Joseph Mullins # Answer: 232792560 # # compile with gcc euler_5.s .global main .text main: mov $20, %r9 next_number: add $20, %r9 # Increment by 20, anything divisble of 1 to 20... must be a multiple of 20 mov $2, %rbx # start the dividing at 2... everyhings divisble by 1 check_if_divisible_everything_under_20: mov $0, %rdx mov %r9, %rax div %rbx cmp $0, %rdx jne next_number cmp $20, %rbx je print inc %rbx jmp check_if_divisible_everything_under_20 print: mov $format, %rdi # set first paramter of print f, formatting mov %r9, %rsi # set 2nd paramter (the result) xor %rax, %rax # because printf is varargs call printf ret format: .asciz "%200d\n"

232792560

The sum of the squares of the first ten natural numbers is,

1^{2} + 2^{2} + ... + 10^{2} = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^{2} = 55^{2} = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

This was a fun one, though simple it was fun. I misread the question for quite a while and went with summing all the powers of 1..100 and summing them individually then subtracting. Obviously this kept giving the wrong answer! Helps to re-read, instead of just constantly trying to work out where you went wrong. Anyway, too the code...

# https://projecteuler.net/problem=6 # The sum of the squares of the first ten natural numbers is, # # 1^2 + 2^2 + ... + 10^2 = 385 # The square of the sum of the first ten natural numbers is, # # (1 + 2 + ... + 10)^2 = 55^2 = 3025 # Hence the difference between the sum of the squares of the first ten natural numbers and the square of # the sum is 3025 − 385 = 2640. # # Find the difference between the sum of the squares of the first one hundred natural numbers and the square # of the sum. # # Solution by Joseph Mullins # Answer: 25164150 # # Compile with gcc euler_6.s .global main .text main: mov $1, %r9 # Sum mov $1, %r10 # Multiples mov $1, %rbx start_incrementing: inc %rbx add %rbx, %r9 # Add to the sum mov %rbx, %rax # move current counter into %rax mul %rbx # times by itself ^2 add %rax, %r10 # add to r10 cmp $100, %rbx # If incrementer gets to 100, we have the sum of them all je find_diff jmp start_incrementing find_diff: mov %r9, %rax # now we need the sum of (1..100)^2 mul %r9 sub %r10, %rax # sub the multiples from the total power print: mov $format, %rdi # set first paramter of print f, formatting mov %rax, %rsi # set 2nd paramter (the result) xor %rax, %rax # because printf is varargs call printf ret format: .asciz "%200d\n"

25164150

Thanks for reading and goodluck on solving these problems yourself! Until next time…

Please sign in to post comments…