SHARE
TWEET

Untitled

a guest Jun 19th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 試割法.定義通り
  2.  
  3. import Math._
  4.  
  5. def isPrime(num: Int): Boolean = {
  6.   def func(num : Int):  Boolean = num match {
  7.     case 2 =>  true
  8.     case n if n < 2 => false
  9.     case n if n%2 == 0 =>  false
  10.     case n => loop(n, 3)
  11.   }
  12.  
  13.   def loop(m : Int, i: Int): Boolean = (m , i) match {
  14.     case (m, i) if m%i == 0 => false
  15.     case (m, i) if pow(i, 2) > m => true
  16.     case (m, i) => loop(m, i + 2)
  17.   }
  18.  
  19.   func(num)
  20. }
  21.  
  22. val result = isPrime(100000000)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top