Ormiston triples: Difference between revisions

Added FreeBASIC
(New post which does not use external libraries. In addition to an existing post which uses the "Boost" library.)
(Added FreeBASIC)
 
(12 intermediate revisions by 4 users not shown)
Line 229:
class SegmentedPrimeIterator {
public:
SegmentedPrimeIterator(const uint64_t& aLimit) {
square_root = std::sqrt(aLimit);
index = 0;
Line 284:
for ( uint32_t p = 2; p <= square_root; ++p ) {
if ( marked_prime[p] ) {
primes.emplace_back((uint64_t) p);
}
}
Line 844:
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num mod 3 = 0
return 0
.
i = 5
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
if num mod i = 0
return 0
.
i += 4
.
return 1
.
func nextprim n .
repeat
n += 2
until isprim n = 1
.
return n
.
func digs n .
while n > 0
r += pow 10 (n mod 10)
n = n div 10
.
return r
.
print "Smallest member of the first 25 Ormiston triples:"
b = 2
a = 3
repeat
c = b
dc = db
b = a
db = da
a = nextprim a
until a > 1000000000
da = digs a
if da = db and da = dc
cnt += 1
if cnt <= 25
write c & " "
.
.
.
print "Ormiston triples before 1 billion: " & cnt
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 860 ⟶ 914:
<1 billion: 368
</pre>
 
=={{header|FreeBASIC}}==
{{trans|XPLo}}
<syntaxhighlight lang="vbnet">Function GetSig(Byval N As Uinteger) As Uinteger 'Return signature of N
'A "signature" is the count of each digit in N packed into a 32-bit word
Dim As Uinteger Sig = 0
Do While N > 0
Sig += 1 Shl (N Mod 10)
N \= 10
Loop
Return Sig
End Function
 
Dim As Uinteger limite = 1e9
Dim Shared As Boolean Sieve(limite)
 
Sub MakeSieve(Size As Uinteger) 'Make prime number sieve
Dim As Uinteger Prime, i, K
Size \= 2 'ignore even numbers
For i = 0 To Size 'set Sieve flags all true
Sieve(i) = True
Next
For i = 0 To Size
If Sieve(i) Then 'found a prime, which is equal to
Prime = i * 2 + 3 ' twice the index + 3
K = i + Prime 'first multiple to strike off
While K <= Size 'strike off all multiples
Sieve(K) = False
K += Prime
Wend
End If
Next
End Sub
 
MakeSieve(limite)
Print "Smallest members of first 25 Ormiston triples:"
Dim As Uinteger Cnt = 0, N0 = 0, N1 = 0, Sig0 = 0, Sig1 = 0, N = 3, Sig
Dim As Double t0 = Timer
Do
If Sieve(N \ 2 - 1) Then ' is prime
Sig = GetSig(N)
If Sig = Sig0 Andalso Sig = Sig1 Then
Cnt += 1
If Cnt <= 25 Then
Print Using "#,###,###,###"; N0;
If Cnt Mod 5 = 0 Then Print
End If
End If
Sig0 = Sig1: Sig1 = Sig
N0 = N1: N1 = N
End If
If N >= limite Then Print Cnt; " Ormiston triples before one billion." : Exit Do
N += 2
Loop
 
Sleep</syntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
Line 1,018 ⟶ 1,129:
 
=={{header|Java}}==
This example uses a segmented prime iterator to complete the stretch task in 7560 seconds, without external libraries.
<syntaxhighlight lang="java">
import java.util.ArrayList;
Line 1,060 ⟶ 1,171:
counts.add(count);
System.out.println("The smallest membersmember of the first 25 Ormiston triples:");
for ( int i = 0; i < ormistons.size(); i++ ) {
System.out.print(String.format("%10d%s", ormistons.get(i), ( i % 5 == 4 ? "\n" : "" )));
Line 1,161 ⟶ 1,272:
{{ out }}
<pre>
The smallest membersmember of the first 25 Ormiston triples:
11117123 12980783 14964017 32638213 32964341
33539783 35868013 44058013 46103237 48015013
Line 2,118 ⟶ 2,229:
25 Ormiston triples before one hundred million
368 Ormiston triples before one billion</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby" line>
require 'prime'
 
def all_anagram?(arr)
sorted = arr.first.digits.sort
arr[1..].all?{|a| a.digits.sort == sorted}
end
 
res = Prime.each.each_cons(3).lazy.filter_map{|ar| ar.first if all_anagram?(ar)}.first(25)
res.each_slice(5){|slice| puts slice.join(", ")}
 
n = 1E9
res = Prime.each(n).each_cons(3).count {|ar| all_anagram?(ar)}
puts "\nThere are #{res} Ormiston triples below #{n}"
</syntaxhighlight>
{{out}}
<pre>11117123, 12980783, 14964017, 32638213, 32964341
33539783, 35868013, 44058013, 46103237, 48015013
50324237, 52402783, 58005239, 60601237, 61395239
74699789, 76012879, 78163123, 80905879, 81966341
82324237, 82523017, 83279783, 86050781, 92514341
 
There are 368 Ormiston triples below 1000000000.0
</pre>
 
=={{header|Wren}}==
Line 2,125 ⟶ 2,261:
 
Limiting the search to a billion, takes about 73 seconds (68 seconds using our 'standard' sieve).
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 2,185 ⟶ 2,321:
 
It's also far quicker - 4.4 seconds to search up to 1 billion and 43.4 seconds to search up to 10 billion.
<syntaxhighlight lang="ecmascriptwren">import "./psieve" for Primes
import "./math" for Int
import "./fmt" for Fmt
2,122

edits