Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
43
Task/Prime-decomposition/Scala/prime-decomposition-1.scala
Normal file
43
Task/Prime-decomposition/Scala/prime-decomposition-1.scala
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import annotation.tailrec
|
||||
import collection.parallel.mutable.ParSeq
|
||||
|
||||
object PrimeFactors extends App {
|
||||
def factorize(n: Long): List[Long] = {
|
||||
@tailrec
|
||||
def factors(tuple: (Long, Long, List[Long], Int)): List[Long] = {
|
||||
tuple match {
|
||||
case (1, _, acc, _) => acc
|
||||
case (n, k, acc, _) if (n % k == 0) => factors((n / k, k, acc ++ ParSeq(k), Math.sqrt(n / k).toInt))
|
||||
case (n, k, acc, sqr) if (k < sqr) => factors(n, k + 1, acc, sqr)
|
||||
case (n, k, acc, sqr) if (k >= sqr) => factors((1, k, acc ++ ParSeq(n), 0))
|
||||
}
|
||||
}
|
||||
factors((n, 2, List[Long](), Math.sqrt(n).toInt))
|
||||
}
|
||||
|
||||
def mersenne(p: Int): BigInt = (BigInt(2) pow p) - 1
|
||||
|
||||
def sieve(nums: Stream[Int]): Stream[Int] =
|
||||
Stream.cons(nums.head, sieve((nums.tail) filter (_ % nums.head != 0)))
|
||||
// An infinite stream of primes, lazy evaluation and memo-ized
|
||||
val oddPrimes = sieve(Stream.from(3, 2))
|
||||
def primes = sieve(2 #:: oddPrimes)
|
||||
|
||||
oddPrimes takeWhile (_ <= 59) foreach { p =>
|
||||
{ // Needs some intermediate results for nice formatting
|
||||
val numM = s"M${p}"
|
||||
val nMersenne = mersenne(p).toLong
|
||||
val lit = f"${nMersenne}%30d"
|
||||
|
||||
val datum = System.nanoTime
|
||||
val result = factorize(nMersenne)
|
||||
val mSec = ((System.nanoTime - datum) / 1.0e+6).round
|
||||
|
||||
def decStr = { if (lit.length > 30) f"(M has ${lit.length}%3d dec)" else "" }
|
||||
def sPrime = { if (result.isEmpty) " is a prime number." else "" }
|
||||
|
||||
println(
|
||||
f"$numM%4s = 2^$p%03d - 1 = ${lit}%s${sPrime} ($mSec%,4d msec) composed of ${result.mkString(" × ")}")
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Task/Prime-decomposition/Scala/prime-decomposition-2.scala
Normal file
33
Task/Prime-decomposition/Scala/prime-decomposition-2.scala
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
class PrimeFactors(n: BigInt) extends Iterator[BigInt] {
|
||||
val zero = BigInt(0)
|
||||
val one = BigInt(1)
|
||||
val two = BigInt(2)
|
||||
def isPrime(n: BigInt) = n.isProbablePrime(10)
|
||||
var currentN = n
|
||||
var prime = two
|
||||
|
||||
def nextPrime =
|
||||
if (prime == two) {
|
||||
prime += one
|
||||
} else {
|
||||
prime += two
|
||||
while (!isPrime(prime)) {
|
||||
prime += two
|
||||
if (prime * prime > currentN)
|
||||
prime = currentN
|
||||
}
|
||||
}
|
||||
|
||||
def next = {
|
||||
if (!hasNext)
|
||||
throw new NoSuchElementException("next on empty iterator")
|
||||
|
||||
while(currentN % prime != zero) {
|
||||
nextPrime
|
||||
}
|
||||
currentN /= prime
|
||||
prime
|
||||
}
|
||||
|
||||
def hasNext = currentN != one && currentN > zero
|
||||
}
|
||||
25
Task/Prime-decomposition/Scala/prime-decomposition-3.scala
Normal file
25
Task/Prime-decomposition/Scala/prime-decomposition-3.scala
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
class PrimeFactors[N](n: N)(implicit num: Integral[N]) extends Iterator[N] {
|
||||
import num._
|
||||
val two = one + one
|
||||
var currentN = n
|
||||
var divisor = two
|
||||
|
||||
def next = {
|
||||
if (!hasNext)
|
||||
throw new NoSuchElementException("next on empty iterator")
|
||||
|
||||
while(currentN % divisor != zero) {
|
||||
if (divisor == two)
|
||||
divisor += one
|
||||
else
|
||||
divisor += two
|
||||
|
||||
if (divisor * divisor > currentN)
|
||||
divisor = currentN
|
||||
}
|
||||
currentN /= divisor
|
||||
divisor
|
||||
}
|
||||
|
||||
def hasNext = currentN != one && currentN > zero
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import spire.math.SafeLong
|
||||
import spire.implicits._
|
||||
def pFactors(num: SafeLong): Vector[SafeLong] = Iterator.iterate((Vector[SafeLong](), num, SafeLong(2))){case (ac, n, f) => if(n%f == 0) (ac :+ f, n/f, f) else (ac, n, f + 1)}.dropWhile(_._2 != 1).next._1
|
||||
Loading…
Add table
Add a link
Reference in a new issue