Rebol3 Code Examplex


Ormiston pairs

Consecutive prime numbers that are anagrams of each other, such as 1913 and 1931.

Rebol [
    title: "Rosetta code: Ormiston pairs"
    file:  %Ormiston_pairs.r3
    url:   https://rosettacode.org/wiki/Ormiston_pairs
]

next-prime: function [
    "Return the first prime greater than n."
    n [integer!]
][
    i: n + either odd? n [2][1]  ;; step by 2 to stay odd
    forever [
        if prime? i [return i]
        i: i + 2
    ]
]

anagrams?: function [
    "Return true if a and b contain the same digits in any order."
    a [integer!] b [integer!]
][
    (sort form a) = (sort form b)  ;; compare sorted digit strings
]

ormiston?: function [
    "Return true if n is prime and anagram of the next prime."
    n [integer!]
][
    all [prime? n  anagrams? n next-prime n]
]

print as-green "First 30 Ormiston pairs:"
n: 0
i: 2
until [
    if ormiston? i [
        prin ajoin ["[" pad i -5 SP pad next-prime i 5 "] "]
        ++ n
        if zero? n % 5 [print ""]  ;; 5 pairs per line
    ]
    ++ i
    n == 30
]

count: 0
repeat i 1000000 [if ormiston? i [++ count]]
print ["^/Ormiston pairs below 1'000'000:" as-green count]

Output:

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] 

Ormiston pairs below 1'000'000: 382