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 valid_triangle?(a, b, c) | |
(a + b > c) && (a + c > b) && (b + c > a) | |
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 times_table(rows) | |
bigarray = [] | |
1.upto(rows) do |x| | |
newarrayrow=[] | |
1.upto(rows) do |y| | |
newarrayrow << x*y | |
end | |
bigarray << newarrayrow.join(' ') | |
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 numwords any_number | |
bignumwords any_number, [] | |
end | |
def bignumwords number, big_array | |
place_names = {1 => 'thousand', | |
2 => 'million', | |
3 => 'billion', | |
4 => 'trillion'} | |
comma_count = Math.log(number,10).floor / 3 |
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 digitsum num | |
currentsum = 0 | |
(Math.log(num,10).ceil).downto(0) do |digit| | |
currentsum = currentsum + num/(10**digit) | |
num = num%(10**digit) | |
end | |
currentsum | |
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 countofmultiples (num,firstfactor,secondfactor) | |
count = 0 | |
1.upto(num-1) do |check| | |
if check % firstfactor == 0 || check % secondfactor == 0 | |
count += check | |
end | |
end | |
count | |
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 sumofprimesbelow number | |
prime_array = [3,5] | |
current_num = 7 | |
while current_num < number | |
prime_array.each do |factor| | |
if current_num % factor == 0 | |
break | |
elsif factor > Math.sqrt(current_num) | |
prime_array << current_num | |
break |
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 largestproduct(nums_as_a_string) | |
currentmax = 0 | |
arry = nums_as_a_string.split("\n") | |
arry.map! { |row_as_a_string| row_as_a_string.split.map! { |digit| digit.to_i } } | |
0.upto( (arry.length - 1).to_i ) do |row| | |
0.upto( (arry[row].length - 1).to_i ) do |col| | |
if col <= (arry[row].length - 4) | |
rowprod = arry[row][col]*arry[row][col+1]*arry[row][col+2]*arry[row][col+3] | |
currentmax = [currentmax,rowprod].max | |
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_fibonacci?(num) | |
phi = (1 + 5.0**0.5)/2 | |
which_fib = ((Math.log(num)+Math.log(5.0**0.5)) / Math.log(phi)).round | |
num == fibonacci(which_fib) | |
end | |
def fibonacci(n) | |
if n==0 | |
0 |
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
whichtrinum = 1 | |
while true | |
trinum = (whichtrinum * (whichtrinum + 1)) / 2 | |
numtofactor = trinum | |
factors = [] | |
factor = 2 | |
while numtofactor != 1 | |
if numtofactor % factor == 0 | |
factors << factor | |
numtofactor = numtofactor / factor |
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 roman_to_integer string | |
string.upcase! | |
raise ArgumentError if /[MDCLXVI]*/.match(string).to_s != string | |
letter_values = {'M' => 1000, | |
'D' => 500, | |
'C' => 100, | |
'L' => 50, | |
'X' => 10, | |
'V' => 5, | |
'I' => 1} |
OlderNewer