Ormiston pairs: Difference between revisions

→‎{{header|Haskell}}: Fractionally more efficient
No edit summary
(→‎{{header|Haskell}}: Fractionally more efficient)
(8 intermediate revisions by 7 users not shown)
Line 467:
<syntaxhighlight lang="Delphi">
 
{------- These subroutines would normally be in libraries, but they are included here for clairty------------- }
 
function IsPrime(N: int64): boolean;
Line 496 ⟶ 497:
Result:=Start;
end;
 
{-----------------------------------------------------------------------------------------------------}
 
 
Line 618 ⟶ 621:
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 798 ⟶ 800:
326,926 Ormiston pairs before 1,000,000,000
</pre>
 
=={{header|Haskell}}==
 
<syntaxhighlight lang=haskell>import Data.List (sort)
import Data.Numbers.Primes (primes)
 
---------------------- ORMISTON PAIRS --------------------
 
ormistonPairs :: [(Int, Int)]
ormistonPairs =
[ (fst a, fst b)
| (a, b) <- zip primeDigits (tail primeDigits),
snd a == snd b
]
 
primeDigits :: [(Int, String)]
primeDigits = (,) <*> (sort . show) <$> primes
 
--------------------------- TEST -------------------------
main :: IO ()
main =
putStrLn "First 30 Ormiston pairs:"
>> mapM_ print (take 30 ormistonPairs)
>> putStrLn "\nCount of Ormistons up to 1,000,000:"
>> print (length (takeWhile ((<= 1000000) . snd) ormistonPairs))</syntaxhighlight>
 
{{Out}}
<pre>First 30 Ormiston pairs:
(1913,1931)
(18379,18397)
(19013,19031)
(25013,25031)
(34613,34631)
(35617,35671)
(35879,35897)
(36979,36997)
(37379,37397)
(37813,37831)
(40013,40031)
(40213,40231)
(40639,40693)
(45613,45631)
(48091,48109)
(49279,49297)
(51613,51631)
(55313,55331)
(56179,56197)
(56713,56731)
(58613,58631)
(63079,63097)
(63179,63197)
(64091,64109)
(65479,65497)
(66413,66431)
(74779,74797)
(75913,75931)
(76213,76231)
(76579,76597)
 
Count of Ormistons up to 1,000,000:
382</pre>
 
=={{header|J}}==
Line 822 ⟶ 885:
+/isorm i.&.(p:inv) 1e7 NB. number of Ormiston pairs less than 1e7
3722</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Preliminaries'''
<syntaxhighlight lang=jq>
# Input: a positive integer
# Assuming . > 2, return an array, $a, of length .+1 such that
# $a[$i]Output: isan $i ifarray, $ia, isof prime,length and.+1 nullsuch otherwise.that
# $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
# erase(i) sets .[i*j] to false for integral j > 1
def erase($i):
if .[$i] then
reduce (range(2*$i; (1 + length) /; $i)) as $j (.; .[i * $j] = nullfalse)
else .
end;
Line 890 ⟶ 955:
3722 Ormiston pairs before 10000000
</pre>
 
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="Julia">using Primes
 
function testormistons(toshow = 30, lim = 1_000_000)
pri = primes(lim)
csort = [sort!(collect(string(i))) for i in pri]
ormistons = [(pri[i - 1], pri[i]) for i in 2:lastindex(pri) if csort[i - 1] == csort[i]]
println("First $toshow Ormiston pairs under $lim:")
for (i, o) in enumerate(ormistons)
i > toshow && break
print("(", lpad(first(o), 6), lpad(last(o), 6), " )", i % 5 == 0 ? "\n" : " ")
end
println("\n", length(ormistons), " is the count of Ormiston pairs up to one million.")
end
 
testormistons()
</syntaxhighlight>{{out}} Same as Python example.
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim>import std/[algorithm, bitops, math, strformat, strutils]
 
type Sieve = object
data: seq[byte]
 
func `[]`(sieve: Sieve; idx: Positive): bool =
## Return value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
result = sieve.data[iByte].testBit(iBit)
 
func `[]=`(sieve: var Sieve; idx: Positive; val: bool) =
## Set value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
if val: sieve.data[iByte].setBit(iBit)
else: sieve.data[iByte].clearBit(iBit)
 
func newSieve(lim: Positive): Sieve =
## Create a sieve with given maximal index.
result.data = newSeq[byte]((lim + 16) shr 4)
 
func initPrimes(lim: Positive): seq[Natural] =
## Initialize the list of primes from 2 to "lim".
var composite = newSieve(lim)
composite[1] = true
for n in countup(3, sqrt(lim.toFloat).int, 2):
if not composite[n]:
for k in countup(n * n, lim, 2 * n):
composite[k] = true
result.add 2
for n in countup(3, lim, 2):
if not composite[n]:
result.add n
 
let primes = initPrimes(100_000_000)
 
func digits(n: Positive): seq[int] =
## Return the sorted list of digits of "n".
var n = n.Natural
while n != 0:
result.add n mod 10
n = n div 10
result.sort()
 
echo "First 30 Ormiston pairs:"
var count = 0
var limit = 100_000
for i in 1..(primes.len - 2):
let p1 = primes[i]
let p2 = primes[i + 1]
if p1.digits == p2.digits:
inc count
if count <= 30:
stdout.write &"({p1:5}, {p2:5})"
stdout.write if count mod 3 == 0: '\n' else: ' '
if count == 30: echo()
elif p1 >= limit:
echo &"Number of Ormiston pairs below {insertSep($limit)}: {count - 1}"
limit *= 10
if limit == 100_000_000:
break
</syntaxhighlight>
 
{{out}}
<pre>First 30 Ormiston pairs:
( 1913, 1931) (18379, 18397) (19013, 19031)
(25013, 25031) (34613, 34631) (35617, 35671)
(35879, 35897) (36979, 36997) (37379, 37397)
(37813, 37831) (40013, 40031) (40213, 40231)
(40639, 40693) (45613, 45631) (48091, 48109)
(49279, 49297) (51613, 51631) (55313, 55331)
(56179, 56197) (56713, 56731) (58613, 58631)
(63079, 63097) (63179, 63197) (64091, 64109)
(65479, 65497) (66413, 66431) (74779, 74797)
(75913, 75931) (76213, 76231) (76579, 76597)
 
Number of Ormiston pairs below 100_000: 40
Number of Ormiston pairs below 1_000_000: 382
Number of Ormiston pairs below 10_000_000: 3722
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
Line 1,703 ⟶ 1,873:
</pre>
 
 
=={{header|RPL}}==
{{works with|HP|49}}
« →STR { 10 } 0 CON
1 PICK3 SIZE '''FOR''' j
OVER j DUP SUB STR→ 1 +
DUP2 GET 1 + PUT
'''NEXT''' NIP
» '<span style="color:blue">DIGCNT</span>' STO <span style="color:grey">''@ ( n → [ count0 .. count9 ] ) ''</span>
« 0 { } 2 3
'''WHILE''' DUP 1E6 < '''REPEAT'''
'''IF''' DUP2 <span style="color:blue">DIGCNT</span> SWAP <span style="color:blue">DIGCNT</span> == '''THEN'''
'''IF''' PICK3 SIZE 30 < '''THEN'''
DUP2 2 →LIST 1 →LIST 4 ROLL SWAP + UNROT
'''END'''
4 ROLL 1 + 4 ROLLD
'''END'''
NIP DUP NEXTPRIME
'''END''' DROP2 SWAP
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: {{1913 1931} {18379 18397} {19013 19031} {25013 25031} {34613 34631} {35617 35671} {35879 35897} {36979 36997} {37379 37397} {37813 37831} {40013 40031} {40213 40231} {40639 40693} {45613 45631} {48091 48109} {49279 49297} {51613 51631} {55313 55331} {56179 56197} {56713 56731} {58613 58631} {63079 63097} {63179 63197} {64091 64109} {65479 65497} {66413 66431} {74779 74797} {75913 75931} {76213 76231} {76579 76597}}
1: 382
</pre>
 
=={{header|Rust}}==
Line 1,784 ⟶ 1,980:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
9,655

edits