From 1eaf499b950bb54307ef5df66183920828034073 Mon Sep 17 00:00:00 2001 From: Ann Date: Fri, 18 Nov 2016 10:43:27 -0500 Subject: [PATCH 1/7] Add power --- factorial.rb | 10 ++++++++++ power.rb | 8 ++++++++ 2 files changed, 18 insertions(+) create mode 100644 factorial.rb create mode 100644 power.rb diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..1d03a34 --- /dev/null +++ b/factorial.rb @@ -0,0 +1,10 @@ +def factorial(x) + y=1 + while x > 1 do + y = x*(x-1) * y + puts y + x-=1 + end + puts y +end +factorial(5) \ No newline at end of file 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 From df329feee94e680da99fe3dc8e8179baa6bee0dd Mon Sep 17 00:00:00 2001 From: Ann Date: Fri, 18 Nov 2016 10:48:04 -0500 Subject: [PATCH 2/7] Add factorial --- factorial.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/factorial.rb b/factorial.rb index 1d03a34..e7b2a8f 100644 --- a/factorial.rb +++ b/factorial.rb @@ -2,8 +2,7 @@ def factorial(x) y=1 while x > 1 do y = x*(x-1) * y - puts y - x-=1 + x-=2 end puts y end From c7e65d5fd1a2506c0a0110270591ed06da488c87 Mon Sep 17 00:00:00 2001 From: Ann Date: Mon, 28 Nov 2016 10:20:52 -0500 Subject: [PATCH 3/7] Add files from codingPrep folder --- combination.rb | 10 ++++++++++ count.rb | 26 ++++++++++++++++++++++++++ countAdv.rb | 23 +++++++++++++++++++++++ evenFib.rb | 15 +++++++++++++++ isPrime.rb | 17 +++++++++++++++++ multiples.rb | 9 +++++++++ overlap.rb | 40 ++++++++++++++++++++++++++++++++++++++++ pal.rb | 26 ++++++++++++++++++++++++++ prime.rb | 27 +++++++++++++++++++++++++++ unique.rb | 17 +++++++++++++++++ uniques.rb | 17 +++++++++++++++++ 11 files changed, 227 insertions(+) create mode 100644 combination.rb create mode 100644 count.rb create mode 100644 countAdv.rb create mode 100644 evenFib.rb create mode 100644 isPrime.rb create mode 100644 multiples.rb create mode 100644 overlap.rb create mode 100644 pal.rb create mode 100644 prime.rb create mode 100644 unique.rb create mode 100644 uniques.rb 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/isPrime.rb b/isPrime.rb new file mode 100644 index 0000000..89996fb --- /dev/null +++ b/isPrime.rb @@ -0,0 +1,17 @@ +def is_prime(value) + if value%2==0 + puts false + return + end + (3..(value/2)).step(2) do |i| + if value%i==0 + puts false + return + end + end + puts true +end + +is_prime(7) + +is_prime(14) \ No newline at end of file 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/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"]) From 0baab933c7c499c4bc17d4e97e2601fd12a5a811 Mon Sep 17 00:00:00 2001 From: Ann Allan Date: Tue, 29 Nov 2016 18:00:07 -0500 Subject: [PATCH 4/7] Update isPrime.rb --- isPrime.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/isPrime.rb b/isPrime.rb index 89996fb..6ab74a1 100644 --- a/isPrime.rb +++ b/isPrime.rb @@ -1,17 +1,18 @@ -def is_prime(value) - if value%2==0 - puts false - return - end - (3..(value/2)).step(2) do |i| - if value%i==0 - puts false - return - end - end - puts true +ddef is_prime(n) + 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) \ No newline at end of file +is_prime(14) From d89756f38dc3981cfcbe909345200edb3949830a Mon Sep 17 00:00:00 2001 From: Ann Allan Date: Tue, 29 Nov 2016 18:00:22 -0500 Subject: [PATCH 5/7] Update isPrime.rb --- isPrime.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/isPrime.rb b/isPrime.rb index 6ab74a1..ed30abb 100644 --- a/isPrime.rb +++ b/isPrime.rb @@ -1,4 +1,4 @@ -ddef is_prime(n) +def is_prime(n) if n % 2 == 0 return false end From 9465055d791075601a5c09d4f5a8c7cb2e4a2bed Mon Sep 17 00:00:00 2001 From: Ann Allan Date: Wed, 30 Nov 2016 09:06:55 -0500 Subject: [PATCH 6/7] Update isPrime.rb --- isPrime.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/isPrime.rb b/isPrime.rb index ed30abb..eae06dd 100644 --- a/isPrime.rb +++ b/isPrime.rb @@ -1,4 +1,10 @@ def is_prime(n) + if n == 1 + return false + end + if n == 2 + return true + end if n % 2 == 0 return false end @@ -12,7 +18,6 @@ def is_prime(n) return true end - is_prime(7) is_prime(14) From b35036cd5b89a9094746505c28734bf5dd478878 Mon Sep 17 00:00:00 2001 From: Ann Allan Date: Wed, 30 Nov 2016 09:12:36 -0500 Subject: [PATCH 7/7] Update isPrime.rb --- isPrime.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/isPrime.rb b/isPrime.rb index eae06dd..06223d7 100644 --- a/isPrime.rb +++ b/isPrime.rb @@ -1,5 +1,5 @@ def is_prime(n) - if n == 1 + if n < 2 return false end if n == 2