From bc455a62c56d58eeab636dba6ee77234acfa9661 Mon Sep 17 00:00:00 2001 From: domix80 Date: Sun, 19 Feb 2023 11:30:30 +0100 Subject: [PATCH 1/7] Feat: Project Euler Problem 7 --- DIRECTORY.md | 2 ++ project_euler/problem_007/sol1.rb | 40 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 project_euler/problem_007/sol1.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 51a0e9e7..2fb4b772 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -153,6 +153,8 @@ * [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_004/sol2.rb) * Problem 005 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_005/sol1.rb) + * Problem 007 + * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_007/sol1.rb) * Problem 020 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_020/sol1.rb) * Problem 021 diff --git a/project_euler/problem_007/sol1.rb b/project_euler/problem_007/sol1.rb new file mode 100644 index 00000000..a249557c --- /dev/null +++ b/project_euler/problem_007/sol1.rb @@ -0,0 +1,40 @@ +#Project Euler Problem 7: #https://projecteuler.net/problem=7 +#10001st prime +#By listing the first six prime numbers: 2, 3, 5, 7, 11, #and 13, we +#can see that the 6th prime is 13. +#What is the 10001st prime number? +#References: https://en.wikipedia.org/wiki/Prime_number + +def is_prime?(number) + value = true + if number > 1 and number < 4 + # 2 and 3 are primes + value = true + elsif number < 2 or number % 2 == 0 or number % 3 == 0 + # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes + value = false + end + end_range = (Math.sqrt(number) + 1).to_i + # All primes number are in format of 6k +/- 1 + for i in (5..end_range).step(6) + if number % i == 0 or number % (i + 2) == 0 + value = false + end + end + result = value +end + +def solution?(nth) + primes = Array.new() + num = 2 + while primes.length < nth + if is_prime?(num) + primes.append(num) + end + num += 1 + end + primes[primes.length - 1] +end + +answer = solution?(1001) +p answer \ No newline at end of file From 5ee0eef4ffff80773c59dc680af9a91ec15b50b8 Mon Sep 17 00:00:00 2001 From: domix80 Date: Sun, 19 Feb 2023 11:35:53 +0100 Subject: [PATCH 2/7] Change the testing number --- project_euler/problem_007/sol1.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_007/sol1.rb b/project_euler/problem_007/sol1.rb index a249557c..aba7f60d 100644 --- a/project_euler/problem_007/sol1.rb +++ b/project_euler/problem_007/sol1.rb @@ -36,5 +36,5 @@ def solution?(nth) primes[primes.length - 1] end -answer = solution?(1001) +answer = solution?(10001) p answer \ No newline at end of file From de8a63dbc92578a6a7b9b8a42d30afc75307f497 Mon Sep 17 00:00:00 2001 From: domix80 Date: Mon, 20 Feb 2023 21:14:50 +0100 Subject: [PATCH 3/7] Update DIRECTORY.md --- DIRECTORY.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2fb4b772..51a0e9e7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -153,8 +153,6 @@ * [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_004/sol2.rb) * Problem 005 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_005/sol1.rb) - * Problem 007 - * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_007/sol1.rb) * Problem 020 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_020/sol1.rb) * Problem 021 From b6b58732478c83f0d49e62f87362205f98272127 Mon Sep 17 00:00:00 2001 From: domix80 Date: Tue, 21 Feb 2023 18:17:13 +0100 Subject: [PATCH 4/7] Update sol1.rb Follow the guidelines --- project_euler/problem_007/sol1.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_007/sol1.rb b/project_euler/problem_007/sol1.rb index aba7f60d..20f14c6f 100644 --- a/project_euler/problem_007/sol1.rb +++ b/project_euler/problem_007/sol1.rb @@ -24,7 +24,8 @@ def is_prime?(number) result = value end -def solution?(nth) +def solution?() + nth = 10001 primes = Array.new() num = 2 while primes.length < nth @@ -36,5 +37,5 @@ def solution?(nth) primes[primes.length - 1] end -answer = solution?(10001) +answer = solution?() p answer \ No newline at end of file From 0db648a4d6a61c918044ffd71cd416c636544c1d Mon Sep 17 00:00:00 2001 From: domix80 Date: Tue, 21 Feb 2023 18:39:37 +0100 Subject: [PATCH 5/7] Update sol1.rb --- project_euler/problem_007/sol1.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_007/sol1.rb b/project_euler/problem_007/sol1.rb index 20f14c6f..44bacca8 100644 --- a/project_euler/problem_007/sol1.rb +++ b/project_euler/problem_007/sol1.rb @@ -24,8 +24,7 @@ def is_prime?(number) result = value end -def solution?() - nth = 10001 +def solution(nth = 10001) primes = Array.new() num = 2 while primes.length < nth @@ -37,5 +36,5 @@ def solution?() primes[primes.length - 1] end -answer = solution?() +answer = solution() p answer \ No newline at end of file From 4420ff4172a87348054b5bbd462a2d3f6c2616f7 Mon Sep 17 00:00:00 2001 From: domix80 Date: Thu, 23 Feb 2023 16:02:35 +0100 Subject: [PATCH 6/7] Update project_euler/problem_007/sol1.rb Co-authored-by: Stepfen Shawn --- project_euler/problem_007/sol1.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_007/sol1.rb b/project_euler/problem_007/sol1.rb index 44bacca8..b17f1ad7 100644 --- a/project_euler/problem_007/sol1.rb +++ b/project_euler/problem_007/sol1.rb @@ -1,4 +1,4 @@ -#Project Euler Problem 7: #https://projecteuler.net/problem=7 +#Project Euler Problem 7: https://projecteuler.net/problem=7 #10001st prime #By listing the first six prime numbers: 2, 3, 5, 7, 11, #and 13, we #can see that the 6th prime is 13. From 9c3eb0577e0a55fa69046ae699a932267d1674da Mon Sep 17 00:00:00 2001 From: domix80 Date: Thu, 23 Feb 2023 16:02:42 +0100 Subject: [PATCH 7/7] Update project_euler/problem_007/sol1.rb Co-authored-by: Stepfen Shawn --- project_euler/problem_007/sol1.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_007/sol1.rb b/project_euler/problem_007/sol1.rb index b17f1ad7..1f79cfbc 100644 --- a/project_euler/problem_007/sol1.rb +++ b/project_euler/problem_007/sol1.rb @@ -1,6 +1,6 @@ #Project Euler Problem 7: https://projecteuler.net/problem=7 #10001st prime -#By listing the first six prime numbers: 2, 3, 5, 7, 11, #and 13, we +#By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we #can see that the 6th prime is 13. #What is the 10001st prime number? #References: https://en.wikipedia.org/wiki/Prime_number