Jump to content

Strong and weak primes: Difference between revisions

m (changed the category name.)
Line 954:
320991 strong primes and 321750 weak primes below 10000000.
</pre>
 
=={{header|Scala}}==
This example works entirely with lazily evaluated lists. It starts with a list of primes, and generates a sliding iterator that looks at each triplet of primes. Lists of strong and weak primes are built by applying the given filters then selecting the middle term from each triplet.
<lang scala>object StrongWeakPrimes {
def main(args: Array[String]): Unit = {
val bnd = 1000000
println(
f"""|First 36 Strong Primes: ${strongPrimes.take(36).map(n => f"$n%,d").mkString(", ")}
|Strong Primes < 1,000,000: ${strongPrimes.takeWhile(_ < bnd).size}%,d
|Strong Primes < 10,000,000: ${strongPrimes.takeWhile(_ < 10*bnd).size}%,d
|
|First 37 Weak Primes: ${weakPrimes.take(37).map(n => f"$n%,d").mkString(", ")}
|Weak Primes < 1,000,000: ${weakPrimes.takeWhile(_ < bnd).size}%,d
|Weak Primes < 10,000,000: ${weakPrimes.takeWhile(_ < 10*bnd).size}%,d""".stripMargin)
}
def weakPrimes: LazyList[Int] = primeTrips.filter{case a +: b +: c +: _ => b < (a + c)/2.0}.map(_(1)).to(LazyList)
def strongPrimes: LazyList[Int] = primeTrips.filter{case a +: b +: c +: _ => b > (a + c)/2}.map(_(1)).to(LazyList)
def primeTrips: Iterator[LazyList[Int]] = primes.sliding(3)
def primes: LazyList[Int] = 2 #:: LazyList.from(3, 2).filter(n => !Iterator.range(3, math.sqrt(n).toInt + 1, 2).exists(n%_ == 0))
}</lang>
 
{{out}}
<pre>First 36 Strong Primes: 11, 17, 29, 37, 41, 59, 67, 71, 79, 97, 101, 107, 127, 137, 149, 163, 179, 191, 197, 223, 227, 239, 251, 269, 277, 281, 307, 311, 331, 347, 367, 379, 397, 419, 431, 439
Strong Primes < 1,000,000: 37,723
Strong Primes < 10,000,000: 320,991
 
First 37 Weak Primes: 3, 7, 13, 19, 23, 31, 43, 47, 61, 73, 83, 89, 103, 109, 113, 131, 139, 151, 167, 181, 193, 199, 229, 233, 241, 271, 283, 293, 313, 317, 337, 349, 353, 359, 383, 389, 401
Weak Primes < 1,000,000: 37,780
Weak Primes < 10,000,000: 321,750</pre>
 
=={{header|Sidef}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.