Parallel calculations: Difference between revisions

Add Swift
(Add Swift)
Line 2,096:
sidef parallel.sf 24.46s user 0.02s system 158% cpu 15.436 total
</pre>
 
=={{header|Swift}}==
 
{{libheader|AttaSwift BigInt}}
 
<lang swift>import Foundation
 
extension BinaryInteger {
@inlinable
public func primeDecomposition() -> [Self] {
guard self > 1 else { return [] }
 
func step(_ x: Self) -> Self {
return 1 + (x << 2) - ((x >> 1) << 1)
}
 
let maxQ = Self(Double(self).squareRoot())
var d: Self = 1
var q: Self = self & 1 == 0 ? 2 : 3
 
while q <= maxQ && self % q != 0 {
q = step(d)
d += 1
}
 
return q <= maxQ ? [q] + (self / q).primeDecomposition() : [self]
}
}
 
let numbers = [
112272537195293,
112582718962171,
112272537095293,
115280098190773,
115797840077099,
1099726829285419,
1275792312878611,
BigInt("64921987050997300559")
]
 
func findLargestMinFactor<T: BinaryInteger>(for nums: [T], then: @escaping ((n: T, factors: [T])) -> ()) {
let waiter = DispatchSemaphore(value: 0)
let lock = DispatchSemaphore(value: 1)
var factors = [(n: T, factors: [T])]()
 
DispatchQueue.concurrentPerform(iterations: nums.count) {i in
let n = nums[i]
 
print("Factoring \(n)")
 
let nFacs = n.primeDecomposition().sorted()
 
print("Factored \(n)")
 
lock.wait()
factors.append((n, nFacs))
 
if factors.count == nums.count {
waiter.signal()
}
 
lock.signal()
}
 
waiter.wait()
 
then(factors.sorted(by: { $0.factors.first! > $1.factors.first! }).first!)
}
 
findLargestMinFactor(for: numbers) {res in
let (n, factors) = res
 
print("Number with largest min prime factor: \(n); factors: \(factors)")
 
exit(0)
}
 
dispatchMain()</lang>
 
{{out}}
 
<pre>$ time ./.build/x86_64-apple-macosx/release/Runner
Factoring 112272537195293
Factoring 64921987050997300559
Factoring 112582718962171
Factoring 112272537095293
Factoring 115797840077099
Factoring 115280098190773
Factoring 1099726829285419
Factoring 1275792312878611
Factored 1099726829285419
Factored 112582718962171
Factored 112272537095293
Factored 1275792312878611
Factored 112272537195293
Factored 115280098190773
Factored 115797840077099
Factored 64921987050997300559
Number with largest min prime factor: 64921987050997300559; factors: [736717, 88123373087627]
 
real 0m2.983s
user 0m3.570s
sys 0m0.012s</pre>
 
=={{header|Tcl}}==