This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
NewerOlder