Project euler Problems
Operators
- Assignment
# Assigning memory location to a variable
a = 5
- Arthimetic
+
-
*
// (integer division quotient)
% (modulus remainder)
/ (division)
- Equality
== (equals)
!= (not equals)
- Logical
and
or
not
- Comparision
<
<=
>
>=
Problem 1
- Problem statement
- memory locations
result = 0
limit = 10
index = 1
until index < limit do the following
if (index % 3 == 0) or (index % 5 == 0) then
result = index + result
end
index = index + 1
end
- Steps (Algorithm)
result = 0
limit = 10
Problem 6
- Problem statement
- Steps
sum = 0
square_sum = 0
result = 0
limit = 3
index = 1
until index <= limit do the following
sum = sum + index
square_sum = square_sum + (index * index)
index = index + 1
end
sum_square = sum * sum
result = sum_square - square_sum
print sum_square
Problem 2
- Problem statement
- Steps
a = 1
b = 2
result = 2
limit = 100
until (a + b) < limit
c = a + b
if c%2 == 0 then
result = result + c
a = b
b = c
end
print result
Problem 3
- Problem statement
-
Break into
- largest factor
- prime
-
Steps
- is_factor(number,index)
inputs:
number
index
steps:
return number%index == 0
- Example
is_factor(10,3) => Falseis_factor(100,10) => True - is_prime(number)
inputs: number
output: True if prime False otherwise
steps:
index = 2
result = True
unless index < number do the following
if is_factor(number, index) == True:
result = False
exit
end
index = index + 1
end
return result
- Example
is_prime(10) = False,is_prime(21) => False,is_prime(11) => true - Solution
number = 70
index = number//2
until index > 2 do the following
if is_factor(number,index) and is_prime(index) == true then
print index
exit
end
index = index - 1
end
- Exercise: Try Problem 5 and 9
