Ocena użytkownikóww: ***** / 0
Nadesłany przez Nikodem Solarz, 18 stycznia 2013 11:35
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.potegowanie_wykladnik_calkowity.rb:
#======================================================
#**Implementacja algorytmu potęgowania o dowolnym wykładniku
#**(dodatnim lub ujemnym, całkowitym)
#**Narzew
#**15.01.2013
#**dla portalu algorytm.org
#======================================================
#======================================================
#**Można prościej! :)
#**Ruby ma wgrane potęgowanie poprzez operator **
#**np. 2 ** 6 => 64
#======================================================
#======================================================
#**x => liczba
#**y => potęga
#======================================================
def potega2(x,y)
return 1 if y == 0
result = 1
if y > 0
while y > 0
result = result * x
y -= 1
end
return result
elsif y < 0
while y < 0
result = result * x
y += 1
end
return (1.to_f/result.to_f)
end
end