Skip to content

Instantly share code, notes, and snippets.

@stoffie
Created January 13, 2016 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoffie/70d195a14cd8eacb64df to your computer and use it in GitHub Desktop.
Save stoffie/70d195a14cd8eacb64df to your computer and use it in GitHub Desktop.
# Find the longest string in a vector
# Count the number of upcased letter in a string
# Reverse an hash inside out, ie:
# {a: :b} becomes {b: :a}
# Find if an array has nested arrays
# Find how deep an array is, ie how many arrays are nested in it
# Parse an IPv4 Address
# Encode a string into morse-code (only for some letters)
# Implement the factorial function
def factorial(n)
(1 .. n).inject(:*)
end
# Find the first factorial number greater than m
def factorial_bigger_than(m)
(1 .. Float::INFINITY).lazy.map { |i| factorial(i) }.find { |j| j > m }
end
p factorial_bigger_than(1000)
# If we list all the natural numbers below 10 that are multiples of 3 we get 3, 6, 9. The sum of these multiples is 18.
def sum_of_multiplier(multiplier, max)
range = 1 .. max
range.select { |i| (i % multiplier) == 0 }.inject(:+)
end
# Find the sum of all the multiples of 3 below 1000.
p sum_of_multiplier(3, 1000)
# Multiples of 3 and 5
# 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.
def sum_of_multipliers(multipliers, max)
range = 1 .. max
range.select { |i|
multipliers.any? { |m|
(i % m) == 0
}
}.inject(:+)
end
# Find the sum of all the multiples of 3 or 5 below 1000.
p sum_of_multipliers([3, 5], 1000)
# Implement the fibonacci sequence
# By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
# Determine if a number is prime
#Largest prime factor
#The prime factors of 13195 are 5, 7, 13 and 29.
#What is the largest prime factor of the number 600851475143 ?
#Palindrome number
# determine if a number is palindrome or not
#Largest palindrome product
#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.
# Sum square difference
# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
# Special Pythagorean triplet
# A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
# a2 + b2 = c2
# For example, 32 + 42 = 9 + 16 = 25 = 52.
# There exists exactly one Pythagorean triplet for which a + b + c = 1000.
# Find the product abc.
#Power digit sum
#2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
#What is the sum of the digits of the number 2^1000?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment