Lucas-Lehmer test: Difference between revisions
Content deleted Content added
Add link to original source |
|||
Line 1,317: | Line 1,317: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
[[Category:Scala Implementations]] |
|||
{{works with|Scala|2.9.1}} |
|||
{{libheader|Scala}} |
|||
In accordance with definition of Mersenne primes it could only be a Mersenne number with prime exponent. |
In accordance with definition of Mersenne primes it could only be a Mersenne number with prime exponent. |
||
<lang Scala>object LLT extends App { |
<lang Scala>object LLT extends App { |
||
import Stream._ |
import Stream._ |
||
def primeSieve(s: Stream[Int]): Stream[Int] = |
def primeSieve(s: Stream[Int]): Stream[Int] = |
||
s.head #:: primeSieve(s.tail filter { _ % s.head != 0 }) |
|||
val primes = primeSieve(from(2)) |
|||
def mersenne(p: Int): BigInt = (BigInt(2) pow p)-1 |
|||
def mersenne(p: Int): BigInt = (BigInt(2) pow p) - 1 |
|||
( |
def s(mp: BigInt, p: Int): BigInt = { if (p == 1) 4 else ((s(mp, p - 1) pow 2) - 2) % mp } |
||
⚫ | |||
val upbPrime = 9999 |
|||
println(s"Finding Mersenne primes in M[2..$upbPrime]") |
|||
((primes takeWhile (_ < upbPrime)).par map { p => (p, mersenne(p)) } |
|||
map { p => if (p._1 == 2) (p, 0) else (p, s(p._2, p._1 - 1)) } filter { _._2 == 0 }) |
|||
.foreach { p => |
|||
println(s"prime M${(p._1)._1}: " + |
|||
⚫ | |||
} |
|||
println("That's All Folks!") |
|||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre style="height: 30ex; overflow: scroll"> |
<pre style="height: 30ex; overflow: scroll">Finding Mersenne primes in M[2..9999] |
||
prime M2: 3 |
|||
prime M3: 7 |
prime M3: 7 |
||
prime M5: 31 |
prime M5: 31 |
||
Line 1,353: | Line 1,364: | ||
prime M4423: (1332 digits) |
prime M4423: (1332 digits) |
||
prime M9689: (2917 digits) |
prime M9689: (2917 digits) |
||
prime M9941: (2993 digits) |
prime M9941: (2993 digits) |
||
That's All Folks!</pre> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |