scala-problem31-35

* 声明*
该系列文章来自:http://aperiodic.net/phil/scala/s-99/
大部分内容和原文相同,加入了部分自己的代码。
如有侵权,请及时联系本人。本人将立即删除相关内容。

在接下来的章节,我们将采取不同的策略。声明了一个类-S99Int,和一个隐式转化器,用来从传统的Int转换到S99Int.

下面是该类的最初声明:

package arithmetic {
  class S99Int(val start: Int) {
    import S99Int._
  }

  object S99Int {
    implicit def int2S99Int(i: Int): S99Int = new S99Int(i)
  }
}

并且在每个问题中将给该类添加相关的功能。最终将得到一个完整的S99Int类。

P31 (**) Determine whether a given integer number is prime.

要求

Example:

scala> 7.isPrime
res0: Boolean = true

代码

class S99Int(val start: Int) {
    import S99Int._

    def isPrime: Boolean =
        (start > 1) && (primes.takeWhile(_ <= Math.sqrt(start)).forall(start % _ != 0))
}

object S99Int {

    implicit def int2S99Int(i: Int): S99Int = new S99Int(i)

    val primes = Stream.cons(2, Stream.from(3, 2) filter { _.isPrime })
}

P32 (**) Determine the greatest common divisor of two positive integer numbers.

要求

Use Euclid’s algorithm.

scala> gcd(36, 63)
res0: Int = 9

代码

object S99Int {
    def gcd(m: Int, n: Int): Int = if (n == 0) m else gcd(n, m % n)
}

P33 (*) Determine whether two positive integer numbers are coprime.

要求

Two numbers are coprime if their greatest common divisor equals 1.

scala> 35.isCoprimeTo(64)
res0: Boolean = true

代码

class S99Int(val start: Int) {
    import S99Int._

    def isCoprimeTo(x: Int): Boolean = gcd(start, x) == 1
}

P34 (**) Calculate Euler’s totient function phi(m).

要求

Euler’s so-called totient function phi(m) is defined as the number of positive integers r (1 <= r <= m) that are coprime to m.

scala> 10.totient
res0: Int = 4

代码

class S99Int(val start: Int) {
    import S99Int._
    def totient: Int = (1 to start).filter(start.isCoprimeTo(_)).length
}

P35 (**) Determine the prime factors of a given positive integer.

要求

Construct a flat list containing the prime factors in ascending order.

scala> 315.primeFactors
res0: List[Int] = List(3, 3, 5, 7)

代码

class S99Int(val start: Int) {
    import S99Int._

    def primeFactors: List[Int] = {
        def primeFactorsR(x: Int, ps: Stream[Int]): List[Int] = {
            x match {
                //本身就是素数
                case n if n.isPrime          => List(n)
                //能分解为n*ps.head
                case n if (n % ps.head) == 0 => ps.head :: primeFactorsR(n / ps.head, ps)
                //其他
                case n                       => primeFactorsR(n, ps.tail)
            }
        }
        return primeFactorsR(start, primes)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值