This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_sq_diff(max) | |
sum_sqs = 0 | |
sq_sums = 0 | |
1.upto(max) { |i| sum_sqs += i**2 } | |
1.upto(max) { |i| sq_sums += i } | |
p sq_sums**2 - sum_sqs | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_prime?(num) | |
2.upto(num-1) { |i| return false if ((num % i) == 0) } | |
true | |
end | |
def this_prime(position) | |
results = [] | |
i = 2 | |
until results.count == position | |
results << i if is_prime?(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def largest_product(num) | |
nums = num.to_s.split(//) | |
products = [] | |
i = 5 | |
0.upto(nums.length-1) { |i| | |
products << nums[i].to_i*nums[i-1].to_i*nums[i-2].to_i*nums[i-3].to_i*nums[i-4].to_i | |
} | |
p products.sort.last |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_prime?(num) | |
2.upto(num-1) { |i| return false if ((num % i) == 0) } | |
true | |
end | |
def sum_primes(max) | |
sum = 0 | |
2.upto(max-1) { |i| sum += i if is_prime?(i) } | |
p sum | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def small_multiple(max) | |
range = *(1..max) | |
nums = [] | |
results = [] | |
range.each { |i| range.each { |x| nums << i*x } } | |
nums.each { |i| | |
range.each { |x| | |
answers = [] | |
i % x == 0 ? answers << 'true' : answers << 'false' | |
results << i if !(answers.include? 'false') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_palindrome?(num) | |
num == num.to_s.reverse.to_i ? true : false | |
end | |
def palindromatic | |
range = *(100..999) | |
results = [] | |
range.each { |i| range.each { |x| results << (i*x) if is_palindrome?(i*x) } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def three_five(max) | |
nums = [] | |
1.upto(max-1) { |i| nums << i if i % 3 == 0 || i % 5 == 0 } | |
p nums.reduce(:+) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_prime?(num) | |
2.upto(num-1) { |i| return false if ((num % i) == 0) } | |
true | |
end | |
def largest_prime(num) | |
primes = [] | |
1.upto(num) { |i| primes << i if num % i == 0 && is_prime?(i) } | |
p primes.last |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def power_sum(base, expo) | |
num = base | |
2.upto(expo) { |i| num *= base } | |
sum = 0 | |
num.to_s.split(//).each { |i| sum += i.to_i } | |
p sum | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load libraries | |
require 'rubygems' rescue nil | |
require 'wirble' | |
#require 'json' | |
alias q exit | |
class Object | |
def local_methods | |
(methods - Object.instance_methods).sort |
OlderNewer