Skip to content

Instantly share code, notes, and snippets.

@daiksy
Created November 25, 2012 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daiksy/4143927 to your computer and use it in GitHub Desktop.
Save daiksy/4143927 to your computer and use it in GitHub Desktop.
Project Euler Problem 16
/**
* http://projecteuler.net/problem=16
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2016
*/
val pow = (base: Int, n: Int) => {
def calc(base: Int, n: Int, summary: BigInt): BigInt = {
n match {
case 1 => summary * base
case _ => calc(base, n - 1, summary * base)
}
}
calc(base, n, BigInt(1))
}
pow(2, 1000).toString.toList.map(Character.getNumericValue(_)).sum
BigInt(2).pow(1000).toString.toList.map(Character.getNumericValue(_)).sum
@daiksy
Copy link
Author

daiksy commented Nov 25, 2012

scala.math.pow だと 2^1000なんてデカイ数計算できないから自作したよ。

@kazua
Copy link

kazua commented Nov 27, 2012

BigInt型のpowメソッドを使用されても良いかと・・・

@daiksy
Copy link
Author

daiksy commented Nov 28, 2012

BigIntにpowメソッドあったんですか ! ><

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment