Skip to content

Commit c672adf

Browse files
committed
Feat: Project Euler Problem 10
1 parent e2f6bd9 commit c672adf

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
153153
* [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_004/sol2.rb)
154154
* Problem 005
155155
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_005/sol1.rb)
156+
* Problem 010
157+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_010/sol1.rb)
156158
* Problem 020
157159
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_020/sol1.rb)
158160
* Problem 021

project_euler/problem_010/sol1.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#Project Euler Problem 10: #https://projecteuler.net/problem=10
2+
#Summation of primes
3+
#The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
4+
#Find the sum of all the primes below two million.
5+
#References: - https://en.wikipedia.org/wiki/Prime_number
6+
def is_prime?(number)
7+
value = true
8+
if number > 1 and number < 4
9+
# 2 and 3 are primes
10+
value = true
11+
elsif number < 2 or number % 2 == 0 or number % 3 == 0
12+
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
13+
value = false
14+
end
15+
end_range = (Math.sqrt(number) + 1).to_i
16+
# All primes number are in format of 6k +/- 1
17+
for i in (5..end_range).step(6)
18+
if number % i == 0 or number % (i + 2) == 0
19+
value = false
20+
end
21+
end
22+
result = value
23+
end
24+
25+
def solution?(max_total)
26+
sum = 1
27+
num = 2
28+
value = 1
29+
while num < max_total and value < max_total
30+
if is_prime?(num)
31+
value += num
32+
if value < max_total
33+
sum = value
34+
end
35+
end
36+
num += 1
37+
end
38+
result = sum
39+
end
40+
41+
answer = solution?(2000000)
42+
p answer

0 commit comments

Comments
 (0)