Sequence of primorial primes: Difference between revisions

Content added Content deleted
(Added C++ solution)
(Add Swift)
Line 2,857: Line 2,857:
[1, 2, 3, 4, 5, 6, 11, 13, 24, 66, 68, 75, 167, 171, 172, 287, 310, 352, 384, 457]
[1, 2, 3, 4, 5, 6, 11, 13, 24, 66, 68, 75, 167, 171, 172, 287, 310, 352, 384, 457]
</pre>
</pre>

=={{header|Swift}}==

{{libheader|AttaSwift BigInt}}

<lang swift>import BigInt
import Foundation

extension BinaryInteger {
@inlinable
public var isPrime: Bool {
if self == 0 || self == 1 {
return false
} else if self == 2 {
return true
}

let max = Self(ceil((Double(self).squareRoot())))

for i in stride(from: 2, through: max, by: 1) where self % i == 0 {
return false
}

return true
}
}

let limit = 20
var primorial = 1
var count = 1
var p = 3
var prod = BigInt(2)

print(1, terminator: " ")

while true {
defer {
p += 2
}

guard p.isPrime else {
continue
}

prod *= BigInt(p)
primorial += 1

if (prod + 1).isPrime() || (prod - 1).isPrime() {
print(primorial, terminator: " ")

count += 1

fflush(stdout)

if count == limit {
break
}
}
}</lang>

{{out}}

<pre>1 2 3 4 5 6 11 13 24 66 68 75 167 171 172 287 310 352 384 457</pre>


=={{header|zkl}}==
=={{header|zkl}}==