Skip to content

Instantly share code, notes, and snippets.

View charlesponti's full-sized avatar

Charles Ponti charlesponti

View GitHub Profile
@charlesponti
charlesponti / Euler_6
Last active December 17, 2015 01:18
Project Euler: Problem 6
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
@charlesponti
charlesponti / Euler_7
Created May 6, 2013 18:08
Project Euler: Problem 7
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)
@charlesponti
charlesponti / Euler_8
Created May 6, 2013 18:09
Project Euler: Problem 8
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
@charlesponti
charlesponti / Euler_10
Created May 6, 2013 18:09
Project Euler: Problem 10
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
@charlesponti
charlesponti / Euler_5
Created May 6, 2013 18:11
Project Euler: Problem 5
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')
@charlesponti
charlesponti / Euler_4
Created May 6, 2013 18:13
Project Euler: Problem 4
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) } }
@charlesponti
charlesponti / Euler_1
Created May 7, 2013 14:05
Project Euler: Problem 1
def three_five(max)
nums = []
1.upto(max-1) { |i| nums << i if i % 3 == 0 || i % 5 == 0 }
p nums.reduce(:+)
end
@charlesponti
charlesponti / Euler_3
Created May 7, 2013 16:58
Project Euler: Problem 3
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
@charlesponti
charlesponti / Euler-16.rb
Last active December 17, 2015 04:08
Project Euler: Problem 16
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
# load libraries
require 'rubygems' rescue nil
require 'wirble'
#require 'json'
alias q exit
class Object
def local_methods
(methods - Object.instance_methods).sort