Exercise – 7 Project euler 1
-
problem
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9 . The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000.
-
Solution
number = 1
result = 0
while number < 10:
if number % 3 == 0 or number % 5 == 0
result = result + number
number = number + 1
say result
conditionals and loops in programming
- Conditional statement: we want to execute a line or block based on some condition
line 1
line 2
when <condition>
line 3
line 4
line 5
- Example
number = 5
result = number % 2
if result == 0
say even
else
say odd
- Loops: Execute same line or block multiple times based on some conditions or unconditionally
line 1
line 2
until <condition>
line 3
line 4
line 5
Exercise- 8: Project euler 2
-
Refer Here for problem
-
Lets print fibbonaci sequence
a = 1
b = 2
say a, b
while a + b < 50:
c = a + b
say c
a = b
b = c
- Lets solve sum of even numbers
a = 1
b = 2
result = 2
while a + b < 50:
c = a + b
if c % 2 == 0:
result = result + c
a = b
b = c
say result
- Exercise: Solve euler problem 5
