Drkcore

01 10 2011 Scala Tweet

Ninety-Nine Scala Problems (P33-P34)

S-99を解く。互いに素かどうか判定する問題と、それをつかってオイラーのφ関数を求める

class IntWrapper(n:scala.Int){
  def gcd(a:scala.Int, b:scala.Int): scala.Int =  if (b == 0) a else gcd(b, a % b)
  def isCoprimeTo(i:scala.Int): Boolean = gcd(n,i) == 1
  def totient: scala.Int = (1 to n) filter {(new IntWrapper(n)).isCoprimeTo(_)} length
}

implicit def int2IntWrapper(n:scala.Int) = new IntWrapper(n)

println("s33: " + 35.isCoprimeTo(64))
println("s34: " + 10.totient)

ProductName Scalaスケーラブルプログラミング第2版
Martin Odersky
インプレスジャパン / 4830円 ( 2011-09-27 )


About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021