Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Euler and Ruby Judo solutions #129

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions combination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def combinations(first_array, second_array)
new_array = [ ]
first_array.each do |x|
second_array.each do |y|
new_array.push(x + y)
end
end
puts new_array
end
combinations(["on","in"],["to","rope"])
26 changes: 26 additions & 0 deletions count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PLAYERS = 10
END_COUNT = 100

def count
direction = 1
person = 1
count = 1
until count > END_COUNT do
puts "Person #{person} says #{count}."
count +=1
if count % 7 == 0
direction *= -1
end
if count % 11 == 0
person += direction
end
person += direction
if person < 1
person += PLAYERS
end
if person > PLAYERS
person -= PLAYERS
end
end
end
count
23 changes: 23 additions & 0 deletions countAdv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def count (players, end_count)
direction = 1
person = 1
count = 1
until count > end_count do
puts "Person #{person} says #{count}."
count +=1
if count % 7 == 0
direction *= -1
end
if count % 11 == 0
person += direction
end
person += direction
if person < 1
person += players
end
if person > players
person -= players
end
end
end
count(10, 100)
15 changes: 15 additions & 0 deletions evenFib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def fib(n)
x=1
y=2
sum=2
until y > n do
z= x+y
if z%2 == 0
sum = sum + z
end
x=y
y=z
end
puts sum
end
fib(4000000)
9 changes: 9 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def factorial(x)
y=1
while x > 1 do
y = x*(x-1) * y
x-=2
end
puts y
end
factorial(5)
23 changes: 23 additions & 0 deletions isPrime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def is_prime(n)
if n < 2
return false
end
if n == 2
return true
end
if n % 2 == 0
return false
end
i = 3
while i < n**0.5 + 1
if n % i == 0
return false
end
i += 2
end
return true
end

is_prime(7)

is_prime(14)
9 changes: 9 additions & 0 deletions multiples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sum = 0
for x in (1...1000)
if x%3 == 0
sum = sum+x
elsif x%5 == 0
sum = sum + x
end
puts sum
end
40 changes: 40 additions & 0 deletions overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

BOTTOMLEFT = 0
TOPRIGHT = 1
X = 0
Y = 1

def overlap(a,b)
case true
when (a[BOTTOMLEFT][X] <= b[BOTTOMLEFT][X]) && (a[BOTTOMLEFT][Y] <= b[BOTTOMLEFT][Y])
if (a[TOPRIGHT][X] > b[BOTTOMLEFT][X]) && (a[TOPRIGHT][Y] > b[BOTTOMLEFT][Y])
puts true
else
puts false
end
when (a[BOTTOMLEFT][X] <= b[BOTTOMLEFT][X]) && (a[BOTTOMLEFT][Y] > b[BOTTOMLEFT][Y])
if (a[TOPRIGHT][X] > b[BOTTOMLEFT][X]) && (b[TOPRIGHT][Y] > a[BOTTOMLEFT][Y])
puts true
else
puts false
end
when (a[BOTTOMLEFT][X] > b[BOTTOMLEFT][X]) && (a[BOTTOMLEFT][Y] > b[BOTTOMLEFT][Y])
if (b[BOTTOMLEFT][X] > a[BOTTOMLEFT][X]) && (b[TOPRIGHT][Y] > a[BOTTOMLEFT][Y])
puts true
else
puts false
end
when (a[BOTTOMLEFT][X] > b[BOTTOMLEFT][X]) && (a[BOTTOMLEFT][Y] <= b[BOTTOMLEFT][Y])
if (b[BOTTOMLEFT][X] > a[TOPRIGHT][X]) && (a[TOPRIGHT][Y] > b[BOTTOMLEFT][Y])
puts true
else
puts false
end
else
puts false
end

end

overlap([[0,0],[3,3]],[[1,1],[4,5]])
overlap([[0,0],[1,4]],[[1,1],[3,2]])
26 changes: 26 additions & 0 deletions pal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def largest_palindrome(number_of_digits)
starting_value = (10 ** number_of_digits) -1
biggest_palindrome = 0
i = starting_value
j = starting_value
until i == 0 do
until j == 0 do
value = i * j
if is_palindrome(value)
if value > biggest_palindrome
biggest_palindrome = value
end
end
j -= 1
end
i -=1
j= starting_value
end
puts biggest_palindrome
end

def is_palindrome(value)
return value.to_s == value.to_s.reverse
end

largest_palindrome(3)
8 changes: 8 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def power(x,y)
z=x*x
(y-2).times do
z=z*x
end
puts z
end
power(3,4)
27 changes: 27 additions & 0 deletions prime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def largest_prime_factor(input)
divisor = 1
while true
if input % divisor == 0
factor = input/divisor
if is_prime(factor)
puts factor
return
end
end
divisor +=1
end
end

def is_prime(value)
if value%2==0
return false
end
(3..(value/2)).step(2) do |i|
if value%i==0
return false
end
end
return true
end

largest_prime_factor(600851475143)
17 changes: 17 additions & 0 deletions unique.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def uniques(input_array)
new_array = [ ]
input_array.each do |x|
exists = false
new_array.each do |y|
if (x == y)
exists = true
end
end
if !exists
new_array.push(x)
end
end
puts new_array
end

uniques([1,5,"frog",2,1,3,"frog"])
17 changes: 17 additions & 0 deletions uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def uniques(input)
new = [input[0]]
i = (input.length) -1
j = (new.length) -1
until i == -1
until j == -1
if input[i] != new[j]
new.push(input[i])
end
end
j -=1
end
i -=1
puts new
end

uniques([1,5,"frog", 2,1,3, "frog"])