I wrote this code to find the prime numbers less than the given number i in scala.
def findPrime(i : Int) : List[Int] = i match {
case 2 => List(2)
case _ => {
val primeList = findPrime(i-1)
if(isPrime(i, primeList)) i :: primeList else primeList
}
}
def isPrime(num : Int, prePrimes : List[Int]) : Boolean = prePrimes.forall(num % _ != 0)
But, I got a feeling the findPrime function, especially this part:
case _ => {
val primeList = findPrime(i-1)
if(isPrime(i, primeList)) i :: primeList else primeList
}
is not quite in the functional style.
I am still learning functional programming. Can anyone please help me improve this code to make it more functional.
Many thanks.
The style looks fine to me. Although the Sieve of Eratosthenes is a very efficient way to find prime numbers, your approach works well too, since you are only testing for division against known primes. You need to watch out however–your recursive function is not tail recursive. A tail recursive function does not modify the result of the recursive call–in your example you prepend to the result of the recursive call. This means that you will have a long call stack and so findPrime will not work for large i. Here is a tail-recursive solution.
This solution isn’t prettier–it’s more of a detail to get your solution to work for large arguments. Since the list is built up backwards to take advantage of fast prepends, the list needs to be reversed. As an alternative, you could use an
Array,Vectoror aListBufferto append the results. With theArray, however, you would need to estimate how much memory to allocate for it. Fortunately we know thatpi(n)is about equal ton / ln(n)so you can choose a reasonable size.ArrayandListBufferare also a mutable data types, which goes again your desire for functional style.Update: to get good performance out of the Sieve of Eratosthenes I think you’ll need to store data in a native array, which also goes against your desire for style in functional programming. There might be a creative functional implementation though!
Update: oops! Missed it! This approach works well too if you only divide by primes less than the square root of the number you are testing! I missed this, and unfortunately it’s not easy to adjust my solution to do this because I’m storing the primes backwards.
Update: here’s a very non-functional solution that at least only checks up to the square root.
rnative, you could use an
Array,Vectoror aListBufferto append the results. With theArray, however, you would need to estimate how much memory to allocate for it. Fortunately we know thatpi(n)is about equal ton / ln(n)so you can choose a reasonable size.ArrayandListBufferare also a mutable data types, which goes again your desire for functional style.Update: to get good performance out of the Sieve of Eratosthenes I think you’ll need to store data in a native array, which also goes against your desire for style in functional programming. There might be a creative functional implementation though!
Update: oops! Missed it! This approach works well too if you only divide by primes less than the square root of the number you are testing! I missed this, and unfortunately it’s not easy to adjust my solution to do this because I’m storing the primes backwards.
Update: here’s a very non-functional solution that at least only checks up to the square root.
Or I could use
Vectors with my original approach.Vectors are probably not the best solution because they don’t have the fasted O(1) even though it’s amortized O(1).