Euler Solution 99

From ProgSoc Wiki

Jump to: navigation, search

Contents

Solutions for Problem 99

Given a file with 1000 base/exponent pairs, find the line number of the largest number.

Haskell by SanguineV

Runtime: 7.536 ms

{- List of base and exponent pairs -}
bxp :: [(Double,Double)]
bxp = [<...>]

{- Zip them with the natural numbers to have line number available -}
nbx = zip [1..] bxp

{- Find the line of the largest number.
 - NOTES:
 - 1. log b^e == e * log b
 - 2. x > y => log x > log y -}
findmax :: [(Integer,(Double,Double))] -> Integer
findmax ns = inner 0 0 ns
  where
    inner line val [] = line
    inner line val ((l,(b,e)):xs)
      | cv > val = inner l cv xs
      | otherwise = inner line val xs
        where
          cv = e * log b

{- Do the search and print the result -}
main = print (findmax nbx)

Ruby by tomchristmas

A simple logarithmic identity...

Runtime: 63ms

num_list = []

count = 0
line_num = 0
greatest = 0.0

File.open("base_exp.txt", "r"){|f|
  f.each{|line|
    num_list << line.chop.split(",")
  }
}

num_list.each{|x|
   count += 1
   y = ((x[1].to_i) * Math.log(x[0].to_f))
   puts y
   if (y > greatest)
      greatest = y
      line_num = count
   end
}

puts line_num

Python by Althalus

Runtime: 1ms

from math import log
base_exps = [
    (519432,525806),
    (632382,518061),
    (78864,613712),
    .....
]
line = 0
res = 0
for i in xrange(len(base_exps)):
    tmp = base_exps[i][1]*log(base_exps[i][0]) # log(a^b) = b* log(a). High school stuff.
    if tmp > res:
        res = tmp
        line = i
print line+1 # Lists are 0 indexed.
Personal tools