From e8105e4e5f09cafece05839a658b75d8b06b406f Mon Sep 17 00:00:00 2001 From: domix80 Date: Sun, 19 Feb 2023 13:05:45 +0100 Subject: [PATCH 1/4] Feat: Project Euler Problem 25 --- DIRECTORY.md | 2 ++ project_euler/problem_025/sol1.rb | 45 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 project_euler/problem_025/sol1.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 51a0e9e..1c733fe 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -159,6 +159,8 @@ * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_021/sol1.rb) * Problem 022 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_022/sol1.rb) + * Problem 025 + * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_025/sol1.rb) ## Searches * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) diff --git a/project_euler/problem_025/sol1.rb b/project_euler/problem_025/sol1.rb new file mode 100644 index 0000000..e13e53b --- /dev/null +++ b/project_euler/problem_025/sol1.rb @@ -0,0 +1,45 @@ +#The Fibonacci sequence is defined by the recurrence relation: +# Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. +#Hence the first 12 terms will be: +# +# F1 = 1 +# F2 = 1 +# F3 = 2 +# F4 = 3 +# F5 = 5 +# F7 = 13 +# F8 = 21 +# F6 = 8 +# F9 = 34 +# F10 = 55 +# F11 = 89 +# F12 = 144 +# +#The 12th term, F12, is the first term to contain three digits. +#What is the index of the first term in the Fibonacci sequence to contain 1000 digits? + +def solution?() + #Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. + resultn1 = 1 + resultn2 = 1 + result = 2 + index = 3 + num_digits = 1000 + value = true + while value + resultn2 = resultn1 + resultn1 = result + if (resultn1 + resultn2).abs.digits.length < num_digits + value = true + else + value = false + end + result = resultn1 + resultn2 + index += 1 + end + res = index +end + +answer = solution?() +p answer + \ No newline at end of file From 1b45e7bc0b80f8345ca2e5cb5d8a1018b802a75f Mon Sep 17 00:00:00 2001 From: domix80 Date: Tue, 21 Feb 2023 18:19:48 +0100 Subject: [PATCH 2/4] Update DIRECTORY.md Remove the link from Directory.md --- DIRECTORY.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1c733fe..d702896 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -158,9 +158,7 @@ * Problem 021 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_021/sol1.rb) * Problem 022 - * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_022/sol1.rb) - * Problem 025 - * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_025/sol1.rb) + * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_022/sol1.rb) ## Searches * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) From 076f202136bbf33a9d2cf0ca452d54be675f6b73 Mon Sep 17 00:00:00 2001 From: domix80 Date: Tue, 21 Feb 2023 18:42:34 +0100 Subject: [PATCH 3/4] Update sol1.rb --- project_euler/problem_025/sol1.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_025/sol1.rb b/project_euler/problem_025/sol1.rb index e13e53b..7a9c7c6 100644 --- a/project_euler/problem_025/sol1.rb +++ b/project_euler/problem_025/sol1.rb @@ -18,13 +18,12 @@ #The 12th term, F12, is the first term to contain three digits. #What is the index of the first term in the Fibonacci sequence to contain 1000 digits? -def solution?() +def solution(num_digits = 1000) #Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. resultn1 = 1 resultn2 = 1 result = 2 index = 3 - num_digits = 1000 value = true while value resultn2 = resultn1 @@ -40,6 +39,6 @@ def solution?() res = index end -answer = solution?() +answer = solution() p answer \ No newline at end of file From 1dbf6795c31103c2ae59cc205b99e8573980f53b Mon Sep 17 00:00:00 2001 From: domix80 Date: Thu, 23 Feb 2023 20:39:16 +0100 Subject: [PATCH 4/4] Update DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index d702896..51a0e9e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -158,7 +158,7 @@ * Problem 021 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_021/sol1.rb) * Problem 022 - * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_022/sol1.rb) + * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_022/sol1.rb) ## Searches * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb)