Skip to content

Commit 2ea0f9a

Browse files
committed
Revert "remove project_euler"
This reverts commit cdc0536.
1 parent 3d9dde9 commit 2ea0f9a

File tree

14 files changed

+294
-0
lines changed

14 files changed

+294
-0
lines changed

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Algorithms in this repo should not be how-to examples for existing Ruby packages
5050

5151
#### Other Requirements for Submissions
5252

53+
- If you are submitting code in the `project_euler/` directory, please also read [the dedicated Guideline](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/README.md) before contributing to our Project Euler library.
5354
- Strictly use snake_case (underscore_separated) in your file_name, as it will be easy to parse in future using scripts.
5455
- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure.
5556
- If possible, follow the standard *within* the folder you are submitting to.

DIRECTORY.md

+20
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@
103103
## Other
104104
* [Fisher Yates](https://github.com/TheAlgorithms/Ruby/blob/master/other/fisher_yates.rb)
105105

106+
## Project Euler
107+
* Problem 1
108+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_1/sol1.rb)
109+
* Problem 2
110+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_2/sol1.rb)
111+
* Problem 20
112+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_20/sol1.rb)
113+
* Problem 21
114+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_21/sol1.rb)
115+
* Problem 22
116+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_22/sol1.rb)
117+
* Problem 3
118+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_3/sol1.rb)
119+
* [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_3/sol2.rb)
120+
* Problem 4
121+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_4/sol1.rb)
122+
* [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_4/sol2.rb)
123+
* Problem 5
124+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_5/sol1.rb)
125+
106126
## Searches
107127
* [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb)
108128
* [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb)

project_euler/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Project Euler
2+
3+
Problems are taken from https://projecteuler.net/, the Project Euler. [Problems are licensed under CC BY-NC-SA 4.0](https://projecteuler.net/copyright).
4+
5+
Project Euler is a series of challenging mathematical/computer programming problems that require more than just mathematical
6+
insights to solve. Project Euler is ideal for mathematicians who are learning to code.
7+
8+
## Solution Guidelines
9+
10+
Welcome to [TheAlgorithms/Ruby](https://github.com/TheAlgorithms/Ruby)! Before reading the solution guidelines, make sure you read the whole [Contributing Guidelines](https://github.com/TheAlgorithms/Ruby/blob/master/CONTRIBUTING.md) as it won't be repeated in here. If you have any doubt on the guidelines, please feel free to [state it clearly in an issue](https://github.com/TheAlgorithms/Ruby/issues/new) or ask the community in [Gitter](https://gitter.im/TheAlgorithms). Be sure to read the [Coding Style](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/README.md#coding-style) before starting solution.
11+
12+
### Coding Style
13+
14+
* Please maintain consistency in project directory and solution file names. Keep the following points in mind:
15+
* Create a new directory only for the problems which do not exist yet.
16+
* Please name the project **directory** as `problem_<problem_number>` where `problem_number` should be filled with 0s so as to occupy 3 digits. Example: `problem_001`, `problem_002`, `problem_067`, `problem_145`, and so on.
17+
18+
* You can have as many helper functions as you want but there should be one main function called `solution` which should satisfy the conditions as stated below:
19+
* It should contain positional argument(s) whose default value is the question input. Example: Please take a look at [Problem 1](https://projecteuler.net/problem=1) where the question is to *Find the sum of all the multiples of 3 or 5 below 1000.* In this case the main solution function will be `solution(limit = 1000)`.
20+
* When the `solution` function is called without any arguments like so: `solution()`, it should return the answer to the problem.

project_euler/problem_1/sol1.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# If we list all the natural numbers below 10 that are multiples of 3 or 5,
2+
# we get 3, 5, 6 and 9. The sum of these multiples is 23.
3+
# Find the sum of all the multiples of 3 or 5 below 1000.
4+
5+
def divisible_by_three_or_five?(number)
6+
(number % 3).zero? || (number % 5).zero?
7+
end
8+
9+
sum = 0
10+
(1...1000).each do |i|
11+
sum += i if divisible_by_three_or_five?(i)
12+
end
13+
14+
p sum

project_euler/problem_2/sol1.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Each new term in the Fibonacci sequence is generated by adding the previous two terms.
2+
# By starting with 1 and 2, the first 10 terms will be:
3+
# 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
4+
# By considering the terms in the Fibonacci sequence whose values do not exceed four million,
5+
# find the sum of the even-valued terms.
6+
7+
even_fib_sum = 0
8+
fib_first = 1
9+
fib_second = 2
10+
11+
while fib_second < 4_000_000
12+
even_fib_sum += fib_second if fib_second.even?
13+
fib_second += fib_first
14+
fib_first = fib_second - fib_first
15+
end
16+
17+
p even_fib_sum

project_euler/problem_20/sol1.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
# n! means n x (n - 1) x ... x 3 x 2 x 1
4+
5+
# For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800,
6+
# and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
7+
#
8+
# Find the sum of the digits in the number 100!
9+
10+
# method to calculate factorial of a number
11+
def factorial(number)
12+
number.downto(1).reduce(:*)
13+
end
14+
15+
# fetch digits of factorial of `number` and find
16+
# sum of all those digits, and prints the result on the console
17+
number = 100
18+
puts factorial(number).digits.sum

project_euler/problem_21/sol1.rb

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
# Let d(n) be defined as the sum of proper divisors of n
4+
# (numbers less than n which divide evenly into n).
5+
# If d(a) = b and d(b) = a, where a & b, then a and b are an amicable pair.
6+
# and each of a and b are called amicable numbers.
7+
#
8+
# For example,
9+
#
10+
# The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110;
11+
# therefore d(220) = 284.
12+
#
13+
# The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
14+
#
15+
# Evaluate the sum of all the amicable numbers under 10000.
16+
17+
# get list of all divisors of `number`
18+
def get_divisors(number)
19+
divisors = []
20+
(1..(Math.sqrt(number).to_i)).each do |num|
21+
if (number % num).zero?
22+
divisors << num
23+
divisors << number / num
24+
end
25+
end
26+
divisors
27+
end
28+
29+
# get list of all proper divisors of `number` i.e. removing `number` from
30+
# the list of divisors
31+
def get_proper_divisors(number)
32+
divisors = get_divisors(number)
33+
divisors.delete(number)
34+
divisors
35+
end
36+
37+
# implementation of a method `d` as mentioned in the question
38+
# i.e. finding sum of all proper divisors of `number`
39+
def d(number)
40+
get_proper_divisors(number).sum
41+
end
42+
43+
# given an upper `limit`, this method finds all amicable numbers
44+
# under this `limit`
45+
def find_amicable_numbers(limit)
46+
result = []
47+
(1...limit).each do |a|
48+
b = d(a)
49+
res = d(b)
50+
result.push(a) if (a == res) && (a != b)
51+
end
52+
result
53+
end
54+
55+
# calling `find_amicable_numbers` method and finding sum of all such numbers
56+
# below 10000, and printing the result on the console
57+
puts find_amicable_numbers(10_000).sum

project_euler/problem_22/p022_names.txt

+1
Large diffs are not rendered by default.

project_euler/problem_22/sol1.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# Problem 22
4+
# Using names.txt (right click and 'Save Link/Target As...'),
5+
# a 46K text file containing over five-thousand first names,
6+
# begin by sorting it into alphabetical order.
7+
# Then working out the alphabetical value for each name,
8+
# multiply this value by its alphabetical position in the list to obtain a name score.
9+
10+
# For example, when the list is sorted into alphabetical order,
11+
# COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53,
12+
# is the 938th name in the list. So, COLIN would obtain a score of 938 * 53 = 49714.
13+
14+
# What is the total of all the name scores in the file?
15+
16+
# reading the contents of the file
17+
file_contents = File.read('p022_names.txt')
18+
19+
# replacing the occuerance of \" to '' and spliting the result by ','
20+
# to get an array of sorted words
21+
words = file_contents.tr('\"', '').split(',').sort
22+
23+
# this method calculates the worth of a word based on the ASCII
24+
# values of the characters
25+
def word_worth(word)
26+
word.chars.sum { |char| char.ord - 'A'.ord + 1 }
27+
end
28+
29+
# this method takes the words as an input
30+
# calls `word_worth` method on each word
31+
# to that value multiply that with the index of the word in the array
32+
# add the same to the result
33+
def total_rank(words)
34+
result = 0
35+
words.each_with_index { |word, index| result += word_worth(word) * (index + 1) }
36+
result
37+
end
38+
39+
# outputs total rank on the console
40+
puts total_rank(words)

project_euler/problem_3/sol1.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
# The prime factors of 13195 are 5, 7, 13 and 29.
4+
# What is the largest prime factor of the number 600851475143
5+
6+
# find all factors of the given number
7+
def get_factors(number)
8+
factors = []
9+
(1..Math.sqrt(number).to_i).each do |num|
10+
if (number % num).zero?
11+
factors << num
12+
factors << number / num
13+
end
14+
end
15+
factors
16+
end
17+
18+
# determine if a given number is a prime number
19+
def prime?(number)
20+
get_factors(number).length == 2
21+
end
22+
23+
# find the largest prime
24+
def largest_prime_factor(number)
25+
prime_factors = get_factors(number).select { |factor| prime?(factor) }
26+
prime_factors.max
27+
end
28+
29+
puts largest_prime_factor(600_851_475_143)

project_euler/problem_3/sol2.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# The prime factors of 13195 are 5, 7, 13 and 29.
2+
# What is the largest prime factor of the number 600851475143 ?
3+
4+
def solution(n)
5+
prime = 1
6+
i = 2
7+
while i * i <= n
8+
while (n % i).zero?
9+
prime = i
10+
n = n.fdiv i
11+
end
12+
i += 1
13+
end
14+
prime = n if n > 1
15+
prime.to_i
16+
end
17+
18+
puts solution(600_851_475_143)

project_euler/problem_4/sol1.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
2+
# Find the largest palindrome made from the product of two 3-digit numbers.
3+
4+
answer = 0
5+
999.downto(99) do |i|
6+
999.downto(99) do |j|
7+
t = (i * j)
8+
answer = i * j if (t.to_s == t.to_s.reverse) && (t > answer) && (t > answer)
9+
end
10+
end
11+
puts answer

project_euler/problem_4/sol2.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
4+
# Find the largest palindrome made from the product of two 3-digit numbers.
5+
6+
class Integer
7+
def parindrome?
8+
self == reverse
9+
end
10+
11+
# 123.reverse == 321
12+
# 100.reverse == 1
13+
def reverse
14+
result = 0
15+
n = self
16+
loop do
17+
result = result * 10 + n % 10
18+
break if (n /= 10).zero?
19+
end
20+
result
21+
end
22+
end
23+
24+
factors = (100..999).to_a
25+
products = factors.product(factors).map { _1 * _2 }
26+
puts products.select(&:parindrome?).max

project_euler/problem_5/sol1.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 2520 is the smallest number that can be divided
2+
# by each of the numbers from 1 to 10 without any remainder.
3+
# What is the smallest positive number that is evenly
4+
# divisible by all of the numbers from 1 to 20?
5+
6+
# Euclid's algorithm for the greatest common divisor
7+
def gcd(a, b)
8+
b.zero? ? a : gcd(b, a % b)
9+
end
10+
11+
# Calculate the LCM using GCD
12+
def lcm(a, b)
13+
(a * b) / gcd(a, b)
14+
end
15+
16+
result = 1
17+
18+
20.times do |i|
19+
result = lcm(result, i + 1)
20+
end
21+
22+
p result

0 commit comments

Comments
 (0)