mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-24 21:58:54 +01:00
parent
3d9dde950b
commit
2ea0f9aacf
14 changed files with 294 additions and 0 deletions
|
@ -50,6 +50,7 @@ Algorithms in this repo should not be how-to examples for existing Ruby packages
|
|||
|
||||
#### Other Requirements for Submissions
|
||||
|
||||
- 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.
|
||||
- Strictly use snake_case (underscore_separated) in your file_name, as it will be easy to parse in future using scripts.
|
||||
- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure.
|
||||
- If possible, follow the standard *within* the folder you are submitting to.
|
||||
|
|
20
DIRECTORY.md
20
DIRECTORY.md
|
@ -103,6 +103,26 @@
|
|||
## Other
|
||||
* [Fisher Yates](https://github.com/TheAlgorithms/Ruby/blob/master/other/fisher_yates.rb)
|
||||
|
||||
## Project Euler
|
||||
* Problem 1
|
||||
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_1/sol1.rb)
|
||||
* Problem 2
|
||||
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_2/sol1.rb)
|
||||
* Problem 20
|
||||
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_20/sol1.rb)
|
||||
* Problem 21
|
||||
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_21/sol1.rb)
|
||||
* Problem 22
|
||||
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_22/sol1.rb)
|
||||
* Problem 3
|
||||
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_3/sol1.rb)
|
||||
* [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_3/sol2.rb)
|
||||
* Problem 4
|
||||
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_4/sol1.rb)
|
||||
* [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_4/sol2.rb)
|
||||
* Problem 5
|
||||
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_5/sol1.rb)
|
||||
|
||||
## Searches
|
||||
* [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb)
|
||||
* [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb)
|
||||
|
|
20
project_euler/README.md
Normal file
20
project_euler/README.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
# Project Euler
|
||||
|
||||
Problems are taken from https://projecteuler.net/, the Project Euler. [Problems are licensed under CC BY-NC-SA 4.0](https://projecteuler.net/copyright).
|
||||
|
||||
Project Euler is a series of challenging mathematical/computer programming problems that require more than just mathematical
|
||||
insights to solve. Project Euler is ideal for mathematicians who are learning to code.
|
||||
|
||||
## Solution Guidelines
|
||||
|
||||
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.
|
||||
|
||||
### Coding Style
|
||||
|
||||
* Please maintain consistency in project directory and solution file names. Keep the following points in mind:
|
||||
* Create a new directory only for the problems which do not exist yet.
|
||||
* 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.
|
||||
|
||||
* 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:
|
||||
* 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)`.
|
||||
* When the `solution` function is called without any arguments like so: `solution()`, it should return the answer to the problem.
|
14
project_euler/problem_1/sol1.rb
Normal file
14
project_euler/problem_1/sol1.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
# If we list all the natural numbers below 10 that are multiples of 3 or 5,
|
||||
# we get 3, 5, 6 and 9. The sum of these multiples is 23.
|
||||
# Find the sum of all the multiples of 3 or 5 below 1000.
|
||||
|
||||
def divisible_by_three_or_five?(number)
|
||||
(number % 3).zero? || (number % 5).zero?
|
||||
end
|
||||
|
||||
sum = 0
|
||||
(1...1000).each do |i|
|
||||
sum += i if divisible_by_three_or_five?(i)
|
||||
end
|
||||
|
||||
p sum
|
17
project_euler/problem_2/sol1.rb
Normal file
17
project_euler/problem_2/sol1.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Each new term in the Fibonacci sequence is generated by adding the previous two terms.
|
||||
# By starting with 1 and 2, the first 10 terms will be:
|
||||
# 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
|
||||
# By considering the terms in the Fibonacci sequence whose values do not exceed four million,
|
||||
# find the sum of the even-valued terms.
|
||||
|
||||
even_fib_sum = 0
|
||||
fib_first = 1
|
||||
fib_second = 2
|
||||
|
||||
while fib_second < 4_000_000
|
||||
even_fib_sum += fib_second if fib_second.even?
|
||||
fib_second += fib_first
|
||||
fib_first = fib_second - fib_first
|
||||
end
|
||||
|
||||
p even_fib_sum
|
18
project_euler/problem_20/sol1.rb
Normal file
18
project_euler/problem_20/sol1.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# n! means n x (n - 1) x ... x 3 x 2 x 1
|
||||
|
||||
# For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800,
|
||||
# and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
|
||||
#
|
||||
# Find the sum of the digits in the number 100!
|
||||
|
||||
# method to calculate factorial of a number
|
||||
def factorial(number)
|
||||
number.downto(1).reduce(:*)
|
||||
end
|
||||
|
||||
# fetch digits of factorial of `number` and find
|
||||
# sum of all those digits, and prints the result on the console
|
||||
number = 100
|
||||
puts factorial(number).digits.sum
|
57
project_euler/problem_21/sol1.rb
Normal file
57
project_euler/problem_21/sol1.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Let d(n) be defined as the sum of proper divisors of n
|
||||
# (numbers less than n which divide evenly into n).
|
||||
# If d(a) = b and d(b) = a, where a & b, then a and b are an amicable pair.
|
||||
# and each of a and b are called amicable numbers.
|
||||
#
|
||||
# For example,
|
||||
#
|
||||
# The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110;
|
||||
# therefore d(220) = 284.
|
||||
#
|
||||
# The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
|
||||
#
|
||||
# Evaluate the sum of all the amicable numbers under 10000.
|
||||
|
||||
# get list of all divisors of `number`
|
||||
def get_divisors(number)
|
||||
divisors = []
|
||||
(1..(Math.sqrt(number).to_i)).each do |num|
|
||||
if (number % num).zero?
|
||||
divisors << num
|
||||
divisors << number / num
|
||||
end
|
||||
end
|
||||
divisors
|
||||
end
|
||||
|
||||
# get list of all proper divisors of `number` i.e. removing `number` from
|
||||
# the list of divisors
|
||||
def get_proper_divisors(number)
|
||||
divisors = get_divisors(number)
|
||||
divisors.delete(number)
|
||||
divisors
|
||||
end
|
||||
|
||||
# implementation of a method `d` as mentioned in the question
|
||||
# i.e. finding sum of all proper divisors of `number`
|
||||
def d(number)
|
||||
get_proper_divisors(number).sum
|
||||
end
|
||||
|
||||
# given an upper `limit`, this method finds all amicable numbers
|
||||
# under this `limit`
|
||||
def find_amicable_numbers(limit)
|
||||
result = []
|
||||
(1...limit).each do |a|
|
||||
b = d(a)
|
||||
res = d(b)
|
||||
result.push(a) if (a == res) && (a != b)
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
# calling `find_amicable_numbers` method and finding sum of all such numbers
|
||||
# below 10000, and printing the result on the console
|
||||
puts find_amicable_numbers(10_000).sum
|
1
project_euler/problem_22/p022_names.txt
Normal file
1
project_euler/problem_22/p022_names.txt
Normal file
File diff suppressed because one or more lines are too long
40
project_euler/problem_22/sol1.rb
Normal file
40
project_euler/problem_22/sol1.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Problem 22
|
||||
# Using names.txt (right click and 'Save Link/Target As...'),
|
||||
# a 46K text file containing over five-thousand first names,
|
||||
# begin by sorting it into alphabetical order.
|
||||
# Then working out the alphabetical value for each name,
|
||||
# multiply this value by its alphabetical position in the list to obtain a name score.
|
||||
|
||||
# For example, when the list is sorted into alphabetical order,
|
||||
# COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53,
|
||||
# is the 938th name in the list. So, COLIN would obtain a score of 938 * 53 = 49714.
|
||||
|
||||
# What is the total of all the name scores in the file?
|
||||
|
||||
# reading the contents of the file
|
||||
file_contents = File.read('p022_names.txt')
|
||||
|
||||
# replacing the occuerance of \" to '' and spliting the result by ','
|
||||
# to get an array of sorted words
|
||||
words = file_contents.tr('\"', '').split(',').sort
|
||||
|
||||
# this method calculates the worth of a word based on the ASCII
|
||||
# values of the characters
|
||||
def word_worth(word)
|
||||
word.chars.sum { |char| char.ord - 'A'.ord + 1 }
|
||||
end
|
||||
|
||||
# this method takes the words as an input
|
||||
# calls `word_worth` method on each word
|
||||
# to that value multiply that with the index of the word in the array
|
||||
# add the same to the result
|
||||
def total_rank(words)
|
||||
result = 0
|
||||
words.each_with_index { |word, index| result += word_worth(word) * (index + 1) }
|
||||
result
|
||||
end
|
||||
|
||||
# outputs total rank on the console
|
||||
puts total_rank(words)
|
29
project_euler/problem_3/sol1.rb
Normal file
29
project_euler/problem_3/sol1.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
# What is the largest prime factor of the number 600851475143
|
||||
|
||||
# find all factors of the given number
|
||||
def get_factors(number)
|
||||
factors = []
|
||||
(1..Math.sqrt(number).to_i).each do |num|
|
||||
if (number % num).zero?
|
||||
factors << num
|
||||
factors << number / num
|
||||
end
|
||||
end
|
||||
factors
|
||||
end
|
||||
|
||||
# determine if a given number is a prime number
|
||||
def prime?(number)
|
||||
get_factors(number).length == 2
|
||||
end
|
||||
|
||||
# find the largest prime
|
||||
def largest_prime_factor(number)
|
||||
prime_factors = get_factors(number).select { |factor| prime?(factor) }
|
||||
prime_factors.max
|
||||
end
|
||||
|
||||
puts largest_prime_factor(600_851_475_143)
|
18
project_euler/problem_3/sol2.rb
Normal file
18
project_euler/problem_3/sol2.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
# What is the largest prime factor of the number 600851475143 ?
|
||||
|
||||
def solution(n)
|
||||
prime = 1
|
||||
i = 2
|
||||
while i * i <= n
|
||||
while (n % i).zero?
|
||||
prime = i
|
||||
n = n.fdiv i
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
prime = n if n > 1
|
||||
prime.to_i
|
||||
end
|
||||
|
||||
puts solution(600_851_475_143)
|
11
project_euler/problem_4/sol1.rb
Normal file
11
project_euler/problem_4/sol1.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
|
||||
# Find the largest palindrome made from the product of two 3-digit numbers.
|
||||
|
||||
answer = 0
|
||||
999.downto(99) do |i|
|
||||
999.downto(99) do |j|
|
||||
t = (i * j)
|
||||
answer = i * j if (t.to_s == t.to_s.reverse) && (t > answer) && (t > answer)
|
||||
end
|
||||
end
|
||||
puts answer
|
26
project_euler/problem_4/sol2.rb
Normal file
26
project_euler/problem_4/sol2.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
|
||||
# Find the largest palindrome made from the product of two 3-digit numbers.
|
||||
|
||||
class Integer
|
||||
def parindrome?
|
||||
self == reverse
|
||||
end
|
||||
|
||||
# 123.reverse == 321
|
||||
# 100.reverse == 1
|
||||
def reverse
|
||||
result = 0
|
||||
n = self
|
||||
loop do
|
||||
result = result * 10 + n % 10
|
||||
break if (n /= 10).zero?
|
||||
end
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
factors = (100..999).to_a
|
||||
products = factors.product(factors).map { _1 * _2 }
|
||||
puts products.select(&:parindrome?).max
|
22
project_euler/problem_5/sol1.rb
Normal file
22
project_euler/problem_5/sol1.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
# 2520 is the smallest number that can be divided
|
||||
# by each of the numbers from 1 to 10 without any remainder.
|
||||
# What is the smallest positive number that is evenly
|
||||
# divisible by all of the numbers from 1 to 20?
|
||||
|
||||
# Euclid's algorithm for the greatest common divisor
|
||||
def gcd(a, b)
|
||||
b.zero? ? a : gcd(b, a % b)
|
||||
end
|
||||
|
||||
# Calculate the LCM using GCD
|
||||
def lcm(a, b)
|
||||
(a * b) / gcd(a, b)
|
||||
end
|
||||
|
||||
result = 1
|
||||
|
||||
20.times do |i|
|
||||
result = lcm(result, i + 1)
|
||||
end
|
||||
|
||||
p result
|
Loading…
Reference in a new issue