Created
November 25, 2012 15:17
-
-
Save daiksy/4143927 to your computer and use it in GitHub Desktop.
Project Euler Problem 16
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
/** | |
* 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 |
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
BigInt(2).pow(1000).toString.toList.map(Character.getNumericValue(_)).sum |
BigInt型のpowメソッドを使用されても良いかと・・・
BigIntにpowメソッドあったんですか ! ><
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
scala.math.pow だと 2^1000なんてデカイ数計算できないから自作したよ。