Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement