Jump to content

Ormiston triples: Difference between revisions

Add Python
m (→‎{{header|Phix}}: blank screen comment)
(Add Python)
Line 297:
Note that running this under pwa/p2js shows 25<1e8 in 8s, a limit of 1e9 would get you a blank screen for 1min 25s<br>
I have not the patience to see whether JavaScript would actually cope with 1e10, but it should (with a blank screen for at least half an hour).
 
=={{header|Python}}==
{{libheader|primesieve}}
 
Using Python bindings for the primesieve C++ library, on my machine, this takes about 58 seconds to find Ormiston triples up to one billion, and just over 9 minutes up to 10 billion.
 
<syntaxhighlight lang="python">import textwrap
 
from collections import Counter
from itertools import pairwise
from typing import Iterator
from typing import List
 
import primesieve
 
 
def primes() -> Iterator[int]:
it = primesieve.Iterator()
while True:
yield it.next_prime()
 
 
def triplewise(iterable):
for (a, _), (b, c) in pairwise(pairwise(iterable)):
yield a, b, c
 
 
def is_anagram(a: int, b: int, c: int) -> bool:
return sorted(str(a)) == sorted(str(b)) == sorted(str(c))
 
 
def up_to_one_billion() -> int:
count = 0
for triple in triplewise(primes()):
if is_anagram(*triple):
count += 1
if triple[2] >= 1_000_000_000:
break
return count
 
 
def up_to_ten_billion() -> int:
count = 0
for triple in triplewise(primes()):
if is_anagram(*triple):
count += 1
if triple[2] >= 10_000_000_000:
break
return count
 
 
def first_25() -> List[int]:
rv: List[int] = []
for triple in triplewise(primes()):
if is_anagram(*triple):
rv.append(triple[0])
if len(rv) >= 25:
break
return rv
 
 
if __name__ == "__main__":
print("Smallest members of first 25 Ormiston triples:")
print(textwrap.fill(" ".join(str(i) for i in first_25())), "\n")
print(up_to_one_billion(), "Ormiston triples before 1,000,000,000")
print(up_to_ten_billion(), "Ormiston triples before 10,000,000,000")</syntaxhighlight>
 
{{out}}
<pre>
Smallest members of first 25 Ormiston triples:
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
 
368 Ormiston triples before 1,000,000,000
4925 Ormiston triples before 10,000,000,000
</pre>
 
=={{header|Wren}}==
140

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.