そんな中、こんなエントリを読んだ。
“素数のときだけ"JOJO!"って出力するプログラムを作ってみた
http://sue445.hatenablog.com/entry/2012/08/11/164950
Rubyスゲーなw
面白そうなのでScalaで書いてみた
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
object jojo extends App { | |
def isPrime(n: Int) = { | |
if (n < 2) { | |
false | |
} else { | |
(2 to n - 1).exists(n % _ == 0) == false | |
} | |
} | |
0 to 100 map {i => | |
if (isPrime(i)) { | |
"JoJo" | |
} else { | |
i | |
} | |
} foreach println | |
} |
もうちょっとエレガントに書けると思うので、他の人のも見てみたい。
0 件のコメント:
コメントを投稿