diff --git a/combination.rb b/combination.rb new file mode 100644 index 0000000..efd2304 --- /dev/null +++ b/combination.rb @@ -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"]) \ No newline at end of file diff --git a/count.rb b/count.rb new file mode 100644 index 0000000..76545e9 --- /dev/null +++ b/count.rb @@ -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 \ No newline at end of file diff --git a/countAdv.rb b/countAdv.rb new file mode 100644 index 0000000..e1018da --- /dev/null +++ b/countAdv.rb @@ -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) \ No newline at end of file diff --git a/evenFib.rb b/evenFib.rb new file mode 100644 index 0000000..4fd071b --- /dev/null +++ b/evenFib.rb @@ -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) diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..e7b2a8f --- /dev/null +++ b/factorial.rb @@ -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) \ No newline at end of file diff --git a/isPrime.rb b/isPrime.rb new file mode 100644 index 0000000..06223d7 --- /dev/null +++ b/isPrime.rb @@ -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) diff --git a/multiples.rb b/multiples.rb new file mode 100644 index 0000000..816fb20 --- /dev/null +++ b/multiples.rb @@ -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 \ No newline at end of file diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..0f74d51 --- /dev/null +++ b/overlap.rb @@ -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]]) \ No newline at end of file diff --git a/pal.rb b/pal.rb new file mode 100644 index 0000000..2a12b4e --- /dev/null +++ b/pal.rb @@ -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) diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..1416eee --- /dev/null +++ b/power.rb @@ -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) \ No newline at end of file diff --git a/prime.rb b/prime.rb new file mode 100644 index 0000000..11891f0 --- /dev/null +++ b/prime.rb @@ -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) \ No newline at end of file diff --git a/unique.rb b/unique.rb new file mode 100644 index 0000000..ae86966 --- /dev/null +++ b/unique.rb @@ -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"]) diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..7f1b861 --- /dev/null +++ b/uniques.rb @@ -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"])