Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. object tutorialOne {
  2. def main(args:Array[String]):Unit = {
  3. def primeFactors(z: Int):List[Int] = {
  4. var a = z
  5. var factors = List[Int]()
  6.  
  7. // Invariant: All prime factors of z up to i-1 are in the list
  8. var i = 2
  9. while (i * i <= a){
  10. // All prime factors equal to i are removed
  11. while (a % i == 0){
  12. factors = i :: factors
  13. a /= i
  14. }
  15. i += 1
  16. }
  17.  
  18. if (a > 1)
  19. factors = a :: factors
  20.  
  21. factors = factors.reverse
  22.  
  23. factors
  24. }
  25. def primeList(input: List[Int]):List[Int] = {
  26. var primes = List[Int]()
  27. val l = input.length
  28.  
  29. // Invariant: All primes in the first i elements of the input
  30. // are in the output list.
  31. var a = List[Int]()
  32. var b:Int = 0
  33. for (i <- 0 to (l-1)) {
  34. a = primeFactors(input(i))
  35. b = a.length
  36. if (b == 1) {input(i) :: primes}
  37. }
  38.  
  39. primes = primes.reverse
  40.  
  41. primes
  42. }
  43. // def bubblesort(ar: Array[Int]):Array[Int] = {
  44. // out = ar;
  45.  
  46. // }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement