Cyclops numbers
You are encouraged to solve this task according to the task description, using any language you may know.
A cyclops number is a number with an odd number of digits that has a zero in the center, but nowhere else. They are named so in tribute to the one eyed giants Cyclops from Greek mythology.
Cyclops numbers can be found in any base. This task strictly is looking for cyclops numbers in base 10.
There are many different classifications of cyclops numbers with minor differences in characteristics. In an effort to head off a whole series of tasks with tiny variations, we will cover several variants here.
- Task
- Find and display here on this page the first 50 cyclops numbers in base 10 (all of the sub tasks are restricted to base 10).
- Find and display here on this page the first 50 prime cyclops numbers. (cyclops numbers that are prime.)
- Find and display here on this page the first 50 blind prime cyclops numbers. (prime cyclops numbers that remain prime when "blinded"; the zero is removed from the center.)
- Find and display here on this page the first 50 palindromic prime cyclops numbers. (prime cyclops numbers that are palindromes.)
- Stretch
- Find and display the first cyclops number greater than ten million (10,000,000) and the index (place) in the series where it is found.
- Find and display the first prime cyclops number greater than ten million (10,000,000) and the index (place) in the series where it is found.
- Find and display the first blind prime cyclops number greater than ten million (10,000,000) and the index (place) in the series where it is found.
- Find and display the first palindromic prime cyclops number greater than ten million (10,000,000) and the index (place) in the series where it is found.
(Note: there are no cyclops numbers between ten million and one hundred million, they need to have an odd number of digits)
- See also
11l
F print50(arr, width = 8)
L(n) arr
print(commatize(n).rjust(width), end' I (L.index + 1) % 10 == 0 {"\n"} E ‘’)
F is_cyclops(=n)
I n == 0
R 1B
V m = n % 10
V count = 0
L m != 0
count++
n I/= 10
m = n % 10
n I/= 10
m = n % 10
L m != 0
count--
n I/= 10
m = n % 10
R n == 0 & count == 0
F is_prime(p)
I p < 2 | p % 2 == 0
R p == 2
L(i) (3 .. Int(sqrt(p))).step(2)
I p % i == 0
R 0B
R 1B
F is_palindromic(d)
R String(d) == reversed(String(d))
[Int] arr
print(‘The first 50 cyclops numbers are:’)
L(i) 0..
I is_cyclops(i)
arr.append(i)
I arr.len == 50
L.break
print50(arr)
print("\nThe first 50 prime cyclops numbers are:")
arr.clear()
L(i) 0..
I is_cyclops(i) & is_prime(i)
arr.append(i)
I arr.len == 50
L.break
print50(arr)
print("\nThe first 50 blind prime cyclops numbers are:")
arr.clear()
L(i) 0..
I is_cyclops(i) & is_prime(i)
V istr = String(i)
V mid = istr.len I/ 2
I is_prime(Int(istr[0 .< mid]‘’istr[mid + 1 ..]))
arr.append(i)
I arr.len == 50
L.break
print50(arr)
print("\nThe first 50 palindromic prime cyclops numbers are:")
arr.clear()
L(i) 0..
I is_cyclops(i) & is_prime(i) & is_palindromic(i)
arr.append(i)
I arr.len == 50
L.break
print50(arr, 11)
- Output:
The first 50 cyclops numbers are: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 The first 50 prime cyclops numbers are: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11,027 11,047 11,057 11,059 11,069 11,071 11,083 11,087 11,093 12,011 12,037 12,041 12,043 12,049 12,071 12,073 12,097 13,033 13,037 13,043 13,049 13,063 13,093 13,099 14,011 14,029 14,033 14,051 14,057 14,071 14,081 14,083 14,087 15,013 15,017 The first 50 blind prime cyclops numbers are: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11,071 11,087 11,093 12,037 12,049 12,097 13,099 14,029 14,033 14,051 14,071 14,081 14,083 14,087 15,031 15,053 15,083 16,057 16,063 16,067 16,069 16,097 17,021 17,033 17,041 17,047 17,053 17,077 18,047 18,061 18,077 18,089 19,013 19,031 19,051 19,073 The first 50 palindromic prime cyclops numbers are: 101 16,061 31,013 35,053 38,083 73,037 74,047 91,019 94,049 1,120,211 1,150,511 1,160,611 1,180,811 1,190,911 1,250,521 1,280,821 1,360,631 1,390,931 1,490,941 1,520,251 1,550,551 1,580,851 1,630,361 1,640,461 1,660,661 1,670,761 1,730,371 1,820,281 1,880,881 1,930,391 1,970,791 3,140,413 3,160,613 3,260,623 3,310,133 3,380,833 3,460,643 3,470,743 3,590,953 3,670,763 3,680,863 3,970,793 7,190,917 7,250,527 7,310,137 7,540,457 7,630,367 7,690,967 7,750,577 7,820,287
ALGOL 68
Generates the sequence.
Note the source of the ALGOL 68-primes library is on a Rosetta Code linked from the above.
BEGIN # show cyclops numbers - numbers with a 0 in the middle and no other 0 digits #
INT max prime = 100 000;
# sieve the primes to max prime #
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE max prime;
# returns TRUE if c is prime, FALSE otherwise #
PROC have a prime = ( INT c )BOOL:
IF c < 2 THEN FALSE
ELIF c < max prime
THEN prime[ c ]
ELSE # the cyclops number is too large for the sieve, use trial division #
BOOL possibly prime := ODD c;
FOR d FROM 3 BY 2 WHILE d * d <= c AND possibly prime DO possibly prime := c MOD d /= 0 OD;
possibly prime
FI # have a prime # ;
# arrays of the cyclops numbers we must show #
[ 1 : 50 ]INT first cyclops; INT cyclops count := 0;
[ 1 : 50 ]INT first prime cyclops; INT prime cyclops count := 0;
[ 1 : 50 ]INT first palindromic prime cyclops; INT palindromic prime cyclops count := 0;
[ 1 : 50 ]INT first blind prime cyclops; INT blind prime cyclops count := 0;
# notes c is a cyclops number, palindromic indicates whether it is palindromic or not #
# bc should be c c with the middle 0 removed #
# if c is one of the first 50 of various classifications, #
# it is stored in the appropriate array #
PROC have cyclops = ( INT c, BOOL palindromic, INT bc )VOID:
BEGIN
cyclops count +:= 1;
IF cyclops count <= UPB first cyclops THEN first cyclops[ cyclops count ] := c FI;
IF prime cyclops count < UPB first prime cyclops
OR ( palindromic prime cyclops count < UPB first palindromic prime cyclops AND palindromic )
OR blind prime cyclops count < UPB first blind prime cyclops
THEN
IF have a prime( c ) THEN
# have a prime cyclops #
IF prime cyclops count < UPB first prime cyclops THEN
first prime cyclops[ prime cyclops count +:= 1 ] := c
FI;
IF palindromic prime cyclops count < UPB first palindromic prime cyclops AND palindromic THEN
first palindromic prime cyclops[ palindromic prime cyclops count +:= 1 ] := c
FI;
IF blind prime cyclops count < UPB first blind prime cyclops THEN
IF have a prime( bc ) THEN
first blind prime cyclops[ blind prime cyclops count +:= 1 ] := c
FI
FI
FI
FI
END # have cyclops # ;
# prints a cyclops sequence #
PROC print cyclops = ( []INT seq, STRING legend, INT elements per line )VOID:
BEGIN
print( ( "The first ", whole( ( UPB seq - LWB seq ) + 1, 0 ), " ", legend, ":", newline, " " ) );
FOR i FROM LWB seq TO UPB seq DO
print( ( " ", whole( seq[ i ], -7 ) ) );
IF i MOD elements per line = 0 THEN print( ( newline, " " ) ) FI
OD;
print( ( newline ) )
END # print cyclops # ;
# generate the cyclops numbers #
# 0 is the first and only cyclops number with less than three digits #
have cyclops( 0, TRUE, 0 );
# generate the 3 digit cyclops numbers #
FOR f TO 9 DO
FOR b TO 9 DO
have cyclops( ( f * 100 ) + b
, f = b
, ( f * 10 ) + b
)
OD
OD;
# generate the 5 digit cyclops numbers #
FOR d1 TO 9 DO
FOR d2 TO 9 DO
INT d1200 = ( ( d1 * 10 ) + d2 ) * 100;
INT d12000 = d1200 * 10;
FOR d4 TO 9 DO
INT d40 = d4 * 10;
FOR d5 TO 9 DO
INT d45 = d40 + d5;
have cyclops( d12000 + d45
, d1 = d5 AND d2 = d4
, d1200 + d45
)
OD
OD
OD
OD;
# generate the 7 digit cyclops numbers #
FOR d1 TO 9 DO
FOR d2 TO 9 DO
FOR d3 TO 9 DO
INT d123000 = ( ( ( ( d1 * 10 ) + d2 ) * 10 ) + d3 ) * 1000;
INT d1230000 = d123000 * 10;
FOR d5 TO 9 DO
INT d500 = d5 * 100;
FOR d6 TO 9 DO
INT d560 = d500 + ( d6 * 10 );
FOR d7 TO 9 DO
INT d567 = d560 + d7;
have cyclops( d1230000 + d567
, d1 = d7 AND d2 = d6 AND d3 = d5
, d123000 + d567
)
OD
OD
OD
OD
OD
OD;
# show parts of the sequence #
print cyclops( first cyclops, "cyclops numbers", 10 );
print cyclops( first prime cyclops, "prime cyclops numbers", 10 );
print cyclops( first blind prime cyclops, "blind prime cyclops numbers", 10 );
print cyclops( first palindromic prime cyclops, "palindromic prime cyclops numbers", 10 )
END
- Output:
The first 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 The first 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 The first 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 The first 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
AppleScript
on task()
script o
property cyclopses : {}
property primeCyclopses : {}
property blindPrimeCyclopses : {}
property palindromicPrimeCyclopses : {}
on add1(digitList)
set carry to 1
repeat with i from (count digitList) to 1 by -1
set columnSum to (digitList's item i) + carry
if (columnSum < 10) then
set digitList's item i to columnSum
return false -- Finish and indicate "no carry".
end if
-- Otherwise set this digit to 1 instead of to 0 and "carry" on. ;)
set digitList's item i to 1
end repeat
return true
end add1
on intFromDigits(digitList)
if (digitList = {}) then return 0
set n to digitList's beginning
repeat with i from 2 to (count digitList)
set n to n * 10 + (digitList's item i)
end repeat
return n
end intFromDigits
on store(cyclops, lst, counter)
if (counter < 51) then
set lst's end to cyclops
else if (cyclops > 10000000) then
set lst's end to {cyclops, counter}
return false -- No more for this list, thanks.
end if
return true
end store
end script
set {leftDigits, rightDigits, rightless, shiftFactor} to {{}, {}, 0, 10}
set {cRef, pcRef, bpcRef, ppcRef} to {a reference to o's cyclopses, ¬
a reference to o's primeCyclopses, a reference to o's blindPrimeCyclopses, ¬
a reference to o's palindromicPrimeCyclopses}
set {cCount, pcCount, bpcCount, ppcCount} to {0, 0, 0, 0}
set {cActive, pcActive, bpcActive, ppcActive} to {true, true, true, true}
repeat while ((bpcActive) or (ppcActive))
set |right| to o's intFromDigits(rightDigits)
set cyclops to rightless + |right|
if (cActive) then
set cCount to cCount + 1
set cActive to o's store(cyclops, cRef, cCount)
end if
if (isPrime(cyclops)) then
if (pcActive) then
set pcCount to pcCount + 1
set pcActive to o's store(cyclops, pcRef, pcCount)
end if
if (bpcActive) then
set blinded to rightless div 10 + |right|
if (isPrime(blinded)) then
set bpcCount to bpcCount + 1
set bpcActive to o's store(cyclops, bpcRef, bpcCount)
end if
end if
if ((ppcActive) and (rightDigits = stigiDtfel)) then
set ppcCount to ppcCount + 1
set ppcActive to o's store(cyclops, ppcRef, ppcCount)
end if
end if
if (o's add1(rightDigits)) then
-- Adding 1 to rightDigits produced a carry. Add 1 to leftDigits too.
if (o's add1(leftDigits)) then
-- Also carried. Extend both lists by 1 digit.
set {leftDigits's beginning, rightDigits's beginning} to {1, 1}
set shiftFactor to shiftFactor * 10
end if
set rightless to (o's intFromDigits(leftDigits)) * shiftFactor
set stigiDtfel to leftDigits's reverse
end if
end repeat
set output to {}
repeat with this in {{"", o's cyclopses}, {"prime ", o's primeCyclopses}, ¬
{"blind prime ", o's blindPrimeCyclopses}, ¬
{"palindromic prime ", o's palindromicPrimeCyclopses}}
set {type, cyclopses} to this
set output's end to linefeed & "First 50 " & type & "cyclops numbers:"
repeat with i from 1 to 50 by 10
set row to {}
repeat with j from i to (i + 9)
set row's end to (" " & cyclopses's item j)'s text -7 thru -1
end repeat
set output's end to join(row, " ")
end repeat
set {nth, n} to cyclopses's end
set output's end to "The first such number > ten million is the " & n & "th: " & nth
end repeat
join(output, linefeed)
end task
on isPrime(n)
if (n < 4) then return (n > 1)
if ((n mod 2 is 0) or (n mod 3 is 0)) then return false
repeat with i from 5 to (n ^ 0.5) div 1 by 6
if ((n mod i is 0) or (n mod (i + 2) is 0)) then return false
end repeat
return true
end isPrime
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
task()
- Output:
"
First 50 cyclops numbers:
0 101 102 103 104 105 106 107 108 109
201 202 203 204 205 206 207 208 209 301
302 303 304 305 306 307 308 309 401 402
403 404 405 406 407 408 409 501 502 503
504 505 506 507 508 509 601 602 603 604
The first such number > ten million is the 538085th: 111101111
First 50 prime cyclops numbers:
101 103 107 109 307 401 409 503 509 601
607 701 709 809 907 11027 11047 11057 11059 11069
11071 11083 11087 11093 12011 12037 12041 12043 12049 12071
12073 12097 13033 13037 13043 13049 13063 13093 13099 14011
14029 14033 14051 14057 14071 14081 14083 14087 15013 15017
The first such number > ten million is the 39320th: 111101129
First 50 blind prime cyclops numbers:
101 103 107 109 307 401 503 509 601 607
701 709 809 907 11071 11087 11093 12037 12049 12097
13099 14029 14033 14051 14071 14081 14083 14087 15031 15053
15083 16057 16063 16067 16069 16097 17021 17033 17041 17047
17053 17077 18047 18061 18077 18089 19013 19031 19051 19073
The first such number > ten million is the 11394th: 111101161
First 50 palindromic prime cyclops numbers:
101 16061 31013 35053 38083 73037 74047 91019 94049 1120211
1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251
1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391
1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763
3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
The first such number > ten million is the 67th: 114808411"
Arturo
<
cyclops?: function [n][
digs: digits n
all? @[
-> odd? size digs
-> zero? digs\[(size digs)/2]
-> 1 = size match to :string n "0"
]
]
blind: function [x][
s: to :string x
half: (size s)/2
to :integer (slice s 0 dec half)++(slice s inc half dec size s)
]
findFirst50: function [what, start, predicate][
print ["First 50" what++"cyclops numbers:"]
first50: new start
i: 100
while [50 > size first50][
if do predicate -> 'first50 ++ i
i: i + 1
]
loop split.every:10 first50 'row [
print map to [:string] row 'item -> pad item 7
]
print ""
]
findFirst50 "" [0] -> cyclops? i
findFirst50 "prime " [] -> and? [prime? i][cyclops? i]
findFirst50 "blind prime " [] -> all? @[
-> prime? i
-> cyclops? i
-> prime? blind i
]
candidates: map 1..999 'x ->
to :integer (to :string x)++"0"++(reverse to :string x)
print "First 50 palindromic prime cyclops numbers:"
loop split.every:10 first.n: 50 select candidates 'x -> and? [prime? x][cyclops? x] 'row [
print map to [:string] row 'item -> pad item 7
]
- Output:
First 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
AWK
<
# syntax: GAWK -f CYCLOPS_NUMBERS.AWK
BEGIN {
n = 0
limit = 50
while (A134808_cnt < limit || A134809_cnt < limit || A329737_cnt < limit || A136098_cnt < limit) {
leng = length(n)
if (leng ~ /[13579]$/) {
middle_col = int(leng/2)+1
if (substr(n,middle_col,1) == 0 && gsub(/0/,"&",n) == 1) {
A134808_arr[++A134808_cnt] = n
if (is_prime(n)) {
A134809_arr[++A134809_cnt] = n
tmp = n
sub(/0/,"",tmp)
if (is_prime(tmp)) {
A329737_arr[++A329737_cnt] = n
}
if (reverse(n) == n) {
A136098_arr[++A136098_cnt] = n
}
}
}
}
n++
}
printf("Range: 0-%d\n\n",n-1)
show_array(A134808_arr,A134808_cnt,"A134808: Cyclops numbers")
show_array(A134809_arr,A134809_cnt,"A134809: Cyclops primes")
show_array(A329737_arr,A329737_cnt,"A329737: Cyclops primes that remain prime after being 'blinded'")
show_array(A136098_arr,A136098_cnt,"A136098: Prime palindromic cyclops numbers")
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
function reverse(str, i,rts) {
for (i=length(str); i>=1; i--) {
rts = rts substr(str,i,1)
}
return(rts)
}
function show_array(arr,cnt,desc, count,i) {
printf("%s Found %d numbers within range\n",desc,cnt)
for (i=1; i<=limit; i++) {
printf("%7d%1s",arr[i],++count%10?"":"\n")
}
printf("\n")
}
- Output:
Range: 0-7820287 A134808: Cyclops numbers Found 407744 numbers within range 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 A134809: Cyclops primes Found 30270 numbers within range 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 A329737: Cyclops primes that remain prime after being 'blinded' Found 8892 numbers within range 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 A136098: Prime palindromic cyclops numbers Found 50 numbers within range 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
BBC BASIC
M% = 50
E% = 10000000
DIM Res%(3, M%-1), Task$(3), Exc%(3), Idx%(3), N% 15
Size%=3
WHILE TRUE
Overflow%=FALSE
Last%=Size% - 1
Zero%=Last% / 2
$$N%=STRING$(Size%, "1")
N%?Zero%=48
REPEAT
V%=VAL$$N%
IF Exc%(0) == 0 THEN
Idx%(0)+=1
IF Idx%(0) < M% Res%(0, Idx%(0))=V%
IF V% > E% Exc%(0)=V%
ENDIF
IF Exc%(2) == 0 THEN
IF FNIsPrime(V%) THEN
IF Exc%(1) == 0 THEN
IF Idx%(1) < M% Res%(1, Idx%(1))=V%
Idx%(1)+=1
IF V% > E% Exc%(1)=V%
ENDIF
IF FNIsPrime(VAL(LEFT$($$N%, Zero%) + $$(N% + Zero% + 1))) THEN
IF Idx%(2) < M% Res%(2, Idx%(2))=V%
Idx%(2)+=1
IF V% > E% Exc%(2)=V%
ENDIF
ENDIF
ENDIF
FOR I%=0 TO Zero%-1
IF N%?I% <> N%?(Last%-I%) EXIT FOR
NEXT
IF I% == Zero% IF FNIsPrime(V%) THEN
IF Idx%(3) < M% Res%(3, Idx%(3))=V%
Idx%(3)+=1
IF V% > E% Exc%(3)=V% EXIT WHILE
ENDIF
D%=Last%
N%?D%+=1
WHILE N%?D% > 57
N%?D%=49
D%-=1 IF D% == Zero% D%-=1
IF D% < 0 Overflow%=TRUE EXIT WHILE
N%?D%+=1
ENDWHILE
UNTIL Overflow%
Size%+=2
ENDWHILE
@%=&20008
Task$()="", " prime", " blind prime", " palindromic prime"
FOR I%=0 TO 3
PRINT "The first ";M% Task$(I%) " cyclop numbers are:"
FOR J%=0 TO M%-1
PRINT Res%(I%, J%),;
IF J% MOD 10 == 9 PRINT
NEXT
PRINT "First" Task$(I%) " cyclop number > ";E% \
\ " is ";Exc%(I%) " at index ";Idx%(I%) "." '
NEXT
END
DEF FNIsPrime(n%) FOR I%=2 TO SQRn% IF n% MOD I% == 0 THEN =FALSE ELSE NEXT =TRUE
- Output:
The first 50 cyclop numbers are: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First cyclop number > 10000000 is 111101111 at index 538084. The first 50 prime cyclop numbers are: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First prime cyclop number > 10000000 is 111101129 at index 39320. The first 50 blind prime cyclop numbers are: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First blind prime cyclop number > 10000000 is 111101161 at index 11394. The first 50 palindromic prime cyclop numbers are: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First palindromic prime cyclop number > 10000000 is 114808411 at index 67.
C++
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
template <class T>
void print(std::vector< T >, size_t);
bool isCyclopsNumber(int);
std::vector< int > firstCyclops(int);
bool isPrime(int);
std::vector< int > firstCyclopsPrimes(int);
int blindCyclops(int);
std::vector< int > firstBlindCyclopsPrimes(int);
bool isPalindrome(int);
std::vector< int > firstPalindromeCyclopsPrimes(int);
int main(void) {
auto first50 = firstCyclops(50);
std::cout << "First 50 cyclops numbers:" << std::endl;
print< int >(first50, 10);
auto prime50 = firstCyclopsPrimes(50);
std::cout << std::endl << "First 50 prime cyclops numbers:" << std::endl;
print< int >(prime50, 10);
auto blind50 = firstBlindCyclopsPrimes(50);
std::cout << std::endl << "First 50 blind prime cyclops numbers:" << std::endl;
print< int >(blind50, 10);
auto palindrome50 = firstPalindromeCyclopsPrimes(50);
std::cout << std::endl << "First 50 palindromic prime cyclops numbers:" << std::endl;
print< int >(palindrome50, 10);
return 0;
}
template <class T>
void print(std::vector< T > v, size_t nc) {
size_t col = 0;
for (auto e : v) {
std::cout << std::setw(8) << e << " ";
col++;
if (col == nc) {
std::cout << std::endl;
col = 0;
}
}
}
bool isCyclopsNumber(int n) {
if (n == 0) {
return true;
}
int m = n % 10;
int count = 0;
while (m != 0) {
count++;
n /= 10;
m = n % 10;
}
n /= 10;
m = n % 10;
while (m != 0) {
count--;
n /= 10;
m = n % 10;
}
return n == 0 && count == 0;
}
std::vector< int > firstCyclops(int n) {
std::vector< int > result;
int i = 0;
while (result.size() < n) {
if (isCyclopsNumber(i)) result.push_back(i);
i++;
}
return result;
}
bool isPrime(int n) {
if (n < 2) return false;
double s = std::sqrt(n);
for (int i = 2; i <= s; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
std::vector< int > firstCyclopsPrimes(int n) {
std::vector< int > result;
int i = 0;
while (result.size() < n) {
if (isCyclopsNumber(i) && isPrime(i)) result.push_back(i);
i++;
}
return result;
}
int blindCyclops(int n) {
int m = n % 10;
int k = 0;
while (m != 0) {
k = 10 * k + m;
n /= 10;
m = n % 10;
}
n /= 10;
while (k != 0) {
m = k % 10;
n = 10 * n + m;
k /= 10;
}
return n;
}
std::vector< int > firstBlindCyclopsPrimes(int n) {
std::vector< int > result;
int i = 0;
while (result.size() < n) {
if (isCyclopsNumber(i) && isPrime(i)) {
int j = blindCyclops(i);
if (isPrime(j)) result.push_back(i);
}
i++;
}
return result;
}
bool isPalindrome(int n) {
int k = 0;
int l = n;
while (l != 0) {
int m = l % 10;
k = 10 * k + m;
l /= 10;
}
return n == k;
}
std::vector< int > firstPalindromeCyclopsPrimes(int n) {
std::vector< int > result;
int i = 0;
while (result.size() < n) {
if (isCyclopsNumber(i) && isPrime(i) && isPalindrome(i)) result.push_back(i);
i++;
}
return result;
}
- Output:
First 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
C#
internal class Program
{
private static void Main(string[] args)
{
long number = 1;
int displayMax = 50;
long highBar = 10_000_000;
List<long> cyclops = [];
int cyclopsCount = 0;
long firstCyclopsOverHighBar = 0;
int firstCyclopsOverHighBarIndex = 0;
List<long> primeCyclops = [];
int primeCyclopsCount = 0;
long firstPrimeCyclopsOverHighBar = 0;
int firstPrimeCyclopsOverHighBarIndex = 0;
List<long> primeBlindCyclops = [];
int primeBlindCyclopsCount = 0;
long firstPrimeBlindCyclopsOverHighBar = 0;
int firstPrimeBlindCyclopsOverHighBarIndex = 0;
List<long> primePalindroneCyclops = [];
int primePalindromCyclopsCount = 0;
long firstPrimePalindromeCyclopsOverHighBar = 0;
int firstPrimePalindromeCyclopsOverHighBarIndex = 0;
while (firstCyclopsOverHighBarIndex == 0 || cyclopsCount < displayMax
|| firstPrimeCyclopsOverHighBarIndex == 0 || primeCyclopsCount < displayMax
|| firstPrimeBlindCyclopsOverHighBarIndex == 0 || primeBlindCyclopsCount < displayMax
|| firstPrimePalindromeCyclopsOverHighBarIndex == 0 || primePalindromCyclopsCount < displayMax)
{
if (IsCyclopsNumber(number))
{
cyclopsCount++;
if (cyclops.Count < displayMax)
{
cyclops.Add(number);
}
if (firstCyclopsOverHighBarIndex == 0 && number > highBar)
{
firstCyclopsOverHighBar = number;
firstCyclopsOverHighBarIndex = cyclopsCount;
}
if (IsPrime(number))
{
primeCyclopsCount++;
if (firstPrimeCyclopsOverHighBarIndex == 0 && number > highBar)
{
firstPrimeCyclopsOverHighBar = number;
firstPrimeCyclopsOverHighBarIndex = primeCyclopsCount;
}
if (primeCyclops.Count < displayMax)
{
primeCyclops.Add(number);
}
if (IsPrime(BlindCyclopsNumber(number)))
{
primeBlindCyclopsCount++;
if (firstPrimeBlindCyclopsOverHighBarIndex == 0 && number > highBar)
{
firstPrimeBlindCyclopsOverHighBar = number;
firstPrimeBlindCyclopsOverHighBarIndex = primeBlindCyclopsCount;
}
if (primeBlindCyclops.Count < displayMax)
{
primeBlindCyclops.Add(number);
}
}
if (IsPalindrome(number.ToString()))
{
primePalindromCyclopsCount++;
if (firstPrimePalindromeCyclopsOverHighBarIndex == 0 && number > highBar)
{
firstPrimePalindromeCyclopsOverHighBar = number;
firstPrimePalindromeCyclopsOverHighBarIndex = primePalindromCyclopsCount;
}
if (primePalindroneCyclops.Count < displayMax)
{
primePalindroneCyclops.Add(number);
}
}
}
}
number++;
}
DisplayNumberSequence(cyclops, $"First {displayMax} Cyclops Numbers", highBar, firstCyclopsOverHighBarIndex, firstCyclopsOverHighBar);
DisplayNumberSequence(primeCyclops, $"First {displayMax} Prime Cyclops Numbers", highBar, firstPrimeCyclopsOverHighBarIndex, firstPrimeCyclopsOverHighBar);
DisplayNumberSequence(primeBlindCyclops, $"First {displayMax} Blind Prime Cyclops Numbers", highBar, firstPrimeBlindCyclopsOverHighBarIndex, firstPrimeBlindCyclopsOverHighBar);
DisplayNumberSequence(primePalindroneCyclops, $"First {displayMax} Palidrome Cyclops Numbers", highBar, firstPrimePalindromeCyclopsOverHighBarIndex, firstPrimePalindromeCyclopsOverHighBar);
}
private static void DisplayNumberSequence(List<long> numbers, string title,
long highBar, long firstIndexOverHighBar, long firstOverHighBar)
{
Console.WriteLine(title);
for (int i = 0; i < numbers.Count; i++)
{
Console.Write($"{numbers[i],-7} ");
if ((i + 1) % 10 == 0)
{
Console.WriteLine();
}
}
Console.WriteLine($"The first such number > {highBar} is the {firstIndexOverHighBar}th: {firstOverHighBar}");
Console.WriteLine();
}
private static bool IsCyclopsNumber(long number )
{
string numberAsString = number.ToString();
int numberLength = numberAsString.Length;
int middleDigit = numberLength / 2;
if (numberLength > 1 && numberLength % 2 !=0 // the number has an odd number of digits greater than one
&& numberAsString.Count(c => c == '0') == 1 // there is only one zero
&& numberAsString[middleDigit] == '0') // Zero is the middle digit
{
return true;
}
return false;
}
private static bool IsPalindrome(string stringInput)
{
if (stringInput != null && stringInput.Length > 0)
{
var reversedString = new string(stringInput.ToCharArray().Reverse().ToArray());
return stringInput == reversedString;
}
return false;
}
private static long BlindCyclopsNumber(long number)
{
string numberAsString = number.ToString();
int numberLength = numberAsString.Length;
int middleDigit = numberLength / 2;
string blindNumber = string.Concat(numberAsString.AsSpan(0, middleDigit), numberAsString.AsSpan(middleDigit + 1, numberLength - (middleDigit + 1)));
return long.Parse(blindNumber);
}
private static bool IsPrime(long number)
{
if (number < 2)
{
return false;
}
if (number % 2 == 0)
{
return number == 2;
}
if (number % 3 == 0)
{
return number == 3;
}
int delta = 2;
long k = 5;
while (k * k <= number)
{
if (number % k == 0)
{
return false;
}
k += delta;
delta = 6 - delta;
}
return true;
}
}
- Output:
First 50 Cyclops Numbers 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 605 The first such number > 10000000 is the 538084th: 111101111 First 50 Prime Cyclops Numbers 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 The first such number > 10000000 is the 39320th: 111101129 First 50 Blind Prime Cyclops Numbers 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 The first such number > 10000000 is the 11394th: 111101161 First 50 Palidrome Cyclops Numbers 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 The first such number > 10000000 is the 67th: 114808411
Delphi
Rather than iterating through all the numbers and picking out the various flavors of Cyclops numbers, this routine construct numbers with a minimum number of tests. The code is also modular so various subroutines reused for different types of cyclops.
var Base: integer; {Base value for iterating through cyclops numbers}
var OutStr: string; {Hold accumulating output data}
function NumberOfDigits(N: integer): integer;
{Find the number of digits in an integer}
begin
if N<1 then Result:=0
else Result:=Trunc(Log10(N))+1;
end;
procedure OutputNumber(Memo: TMemo; N: integer);
{Output number, grouping them 10 items per line}
begin
OutStr:=OutStr+Format('%8D',[N]);
if ((Length(OutStr) div 8) mod 10)=0 then
begin
Memo.Lines.Add(OutStr);
OutStr:='';
end;
end;
function IsPrime(N: integer): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (i + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
function NonZeroIncrement(N: integer): integer;
{Find next number with no zero digits}
begin
repeat Inc(N)
until Pos('0',IntToStr(N))<1;
Result:=N;
end;
function GetNonZeroEvenDigits(N: integer): integer;
{Get next number with no zeros that has even number of digits}
begin
repeat N:=NonZeroIncrement(N)
until (NumberOfDigits(N) and 1)=0;
Result:=N;
end;
function InsertCenterZero(N: integer): integer;
{Insert a zero in the centers of a number}
{assumes the number is an even number digits long}
var S: string;
begin
S:=IntToStr(N);
Insert('0',S,(Length(S) div 2)+1);
Result:=StrToInt(S);
end;
function GetNextCyclops: integer;
{Get next cyclops number}
begin
Base:=GetNonZeroEvenDigits(Base);
Result:=InsertCenterZero(Base);
end;
function GetPalindromeCyclops: integer;
{Get next palindromic cyclops number}
var S1,S2: string;
begin
Base:=NonZeroIncrement(Base);
S1:=IntToStr(Base);
S2:=ReverseString(S1);
Result:=StrToInt(S1+'0'+S2);
end;
{--------------- Top level routines -------------------------------------------}
procedure GetCyclopsNumbers(Memo: TMemo);
{find first 50 Prime Cyclcops Numbers}
var I,C: integer;
var S: string;
begin
Memo.Lines.Add('First 50 Cyclops Numbers');
Base:=0;
OutputNumber(Memo,0);
for I:=1 to 50-1 do
begin
C:=GetNextCyclops;
OutputNumber(Memo,C);
end;
if OutStr<>'' then Memo.Lines.Add(OutStr);
end;
procedure GetPrimeCyclops(Memo: TMemo);
{find first 50 Prime Cyclcops Numbers}
var I,C: integer;
var S: string;
begin
Memo.Lines.Add('First 50 Prime Cyclops Numbers');
Base:=0;
for I:=1 to 50 do
begin
repeat C:=GetNextCyclops;
until IsPrime(C);
OutputNumber(Memo,C);
end;
if OutStr<>'' then Memo.Lines.Add(OutStr);
end;
procedure GetBlindPrimeCyclops(Memo: TMemo);
{find first 50 Blind Prime Cyclcops Numbers}
var I,C,CB: integer;
var S: string;
begin
Memo.Lines.Add('First 50 Blind Prime Cyclops Numbers');
Base:=0;
for I:=1 to 50 do
begin
repeat C:=GetNextCyclops;
until IsPrime(Base);
OutputNumber(Memo,C);
end;
if OutStr<>'' then Memo.Lines.Add(OutStr);
end;
procedure GetPalindromicPrimeCyclops(Memo: TMemo);
{find first 50 Palindromic Prime Cyclcops Numbers}
var I,C,CB: integer;
var S: string;
begin
Memo.Lines.Add('First 50 Palindromic Prime Cyclops Numbers');
Base:=0;
for I:=1 to 50 do
begin
repeat C:=GetPalindromeCyclops;
until IsPrime(C);
OutputNumber(Memo,C);
end;
if OutStr<>'' then Memo.Lines.Add(OutStr);
end;
procedure DisplayCyclopsNumbers(Memo: TMemo);
{Do full suite of Cyclops tasks}
begin
GetCyclopsNumbers(Memo);
GetPrimeCyclops(Memo);
GetBlindPrimeCyclops(Memo);
GetPalindromicPrimeCyclops(Memo);
end;
- Output:
First 50 Cyclops Numbers 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First 50 Prime Cyclops Numbers 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First 50 Blind Prime Cyclops Numbers 101 103 107 109 203 209 301 307 401 403 407 503 509 601 607 701 703 709 803 809 907 11017 11023 11029 11051 11053 11063 11071 11081 11087 11093 12013 12017 12023 12029 12031 12037 12049 12059 12077 12079 12083 12089 12091 12097 13019 13021 13027 13061 13067 First 50 Palindromic Prime Cyclops Numbers 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
EasyLang
func is_cyclops n .
if n = 0
return 1
.
m = n mod 10
while m <> 0
count += 1
n = n div 10
m = n mod 10
.
n = n div 10
m = n mod 10
while m <> 0
count -= 1
n = n div 10
m = n mod 10
.
return if n = 0 and count = 0
.
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func blind n .
m = n mod 10
while m <> 0
k = 10 * k + m
n = n div 10
m = n mod 10
.
n = n div 10
while k <> 0
m = k mod 10
n = 10 * n + m
k = k div 10
.
return n
.
func is_palindr n .
l = n
while l <> 0
m = l mod 10
k = 10 * k + m
l = l div 10
.
return if n = k
.
proc show . .
while cnt < 50
if is_cyclops i = 1
write i & " "
cnt += 1
.
i += 1
.
print ""
print ""
i = 2
cnt = 0
while cnt < 50
if is_cyclops i = 1 and isprim i = 1
write i & " "
cnt += 1
.
i += 1
.
print ""
print ""
i = 2
cnt = 0
while cnt < 50
if is_cyclops i = 1 and isprim i = 1
j = blind i
if isprim j = 1
write i & " "
cnt += 1
.
.
i += 1
.
print ""
print ""
i = 2
cnt = 0
while cnt < 50
if is_cyclops i = 1 and is_palindr i = 1 and isprim i = 1
write i & " "
cnt += 1
.
i += 1
.
.
show
Elixir
defmodule Rosetta do
def cy n do
require Integer
lc1=for cl <- 100..999, d=Integer.digits(cl),[q,y,z]=d,Integer.is_odd(length(d)) and y==0 and q>0 and z>0, do: cl
lc2=for cl <- 10000..99999, d=Integer.digits(cl),[q1,q2,y,z1,z2]=d,Integer.is_odd(length(d)) and y==0 and q1>0 and q2>0 and z1>0 and z2>0, do: cl
lc3=for cl <- 1000000..9999999, d=Integer.digits(cl),[q1,q2,q3,y,z1,z2,z3]=d,Integer.is_odd(length(d)) and y==0 and q1>0 and q2>0 and q3>0 and z1>0 and z2>0 and z3>0, do: cl
lc4=for cl <- 100000000..999999999, d=Integer.digits(cl),[q1,q2,q3,q4,y,z1,z2,z3,z4]=d,Integer.is_odd(length(d)) and y==0 and q1>0 and q2>0 and q3>0 and q4>0 and z1>0 and z2>0 and z3>0 and z4>0, do: cl
lc5=lc1++lc2++lc3++lc4
lc6=[0|lc5]
Enum.take(lc6,n)
end
"50 cyclops num"
iex(8)> cn=Rosetta.cy 50
[0, 101, 102, 103, 104, 105, 106, 107, 108, 109, 201, 202, 203, 204, 205, 206,
207, 208, 209, 301, 302, 303, 304, 305, 306, 307, 308, 309, 401, 402, 403, 404,
405, 406, 407, 408, 409, 501, 502, 503, 504, 505, 506, 507, 508, 509, 601, 602,
603, 604]
def cyp cn,n do
cnp=for x <- cn,x>0,length(Enum.filter(1..x,fn y -> Integer.mod(x,y)==0 end))==2, do: x
Enum.take(cnp,n)
end
"50 cyclops primes"
iex(11)> Rosetta.cyp(Rosetta.cy(1000),50)
[101, 103, 107, 109, 307, 401, 409, 503, 509, 601, 607, 701, 709, 809, 907,
11027, 11047, 11057, 11059, 11069, 11071, 11083, 11087, 11093, 12011, 12037,
12041, 12043, 12049, 12071, 12073, 12097, 13033, 13037, 13043, 13049, 13063,
13093, 13099, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087,
15013, 15017]
def cyz cn,n do
czn=for x <- cn, x>0,do: String.to_integer(String.replace(Integer.to_string(x),"0",""))
Enum.take(czn,n)
end
"50 blind cyclops prime"
iex(13)> Rosetta.cyp(Rosetta.cyz(Rosetta.cy(5000),1000),50)
[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
97, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1213, 1217,
1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1319,
1321, 1327, 1361, 1367]
def cypa cn,n do
czp=for x <- cn,Integer.to_string(x)==String.reverse(Integer.to_string(x)), do: x
Enum.take(czp,n)
end
end
"50 palindrome prime cyclops"
iex(2)> Rosetta.cyp(Rosetta.cypa(Rosetta.cy(3000000),3000),50)
[101, 16061, 31013, 35053, 38083, 73037, 74047, 91019, 94049, 1120211, 1150511,
1160611, 1180811, 1190911, 1250521, 1280821, 1360631, 1390931, 1490941,
1520251, 1550551, 1580851, 1630361, 1640461, 1660661, 1670761, 1730371,
1820281, 1880881, 1930391, 1970791, 3140413, 3160613, 3260623, 3310133,
3380833, 3460643, 3470743, 3590953, 3670763, 3680863, 3970793, 7190917,
7250527, 7310137, 7540457, 7630367, 7690967, 7750577, 7820287]
F#
This task uses Extensible Prime Generator (F#)
Cyclops function
<
// Cyclop numbers. Nigel Galloway: June 25th., 2021
let rec fG n g=seq{yield! g|>Seq.collect(fun i->g|>Seq.map(fun g->n*i+g)); yield! fG(n*10)(fN g)}
let cyclops=seq{yield 0; yield! fG 100 [1..9]}
let primeCyclops,blindCyclops=cyclops|>Seq.filter isPrime,Seq.zip(fG 100 [1..9])(fG 10 [1..9])|>Seq.filter(fun(n,g)->isPrime n && isPrime g)|>Seq.map fst
let palindromicCyclops=let fN g=let rec fN g=[yield g%10; if g>9 then yield! fN(g/10)] in let n=fN g in n=List.rev n in primeCyclops|>Seq.filter fN
The tasks
- First 50 Cyclop numbers
<
cyclops|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
- Output:
0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604
- First 50 prime Cyclop numbers
<
primeCyclops|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
- Output:
101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017
- First 50 blind Cyclop numbers
<
blindCyclops|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
- Output:
101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073
- First 50 palindromic prime Cyclop numbers
<
palindromicCyclops|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
- Output:
101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
- First Cyclop number > 10,000,000
<
let n=cyclops|>Seq.findIndex(fun n->n>10000000) in printfn "First Cyclop number > 10,000,000 is %d at index %d" (Seq.item n (cyclops)) n
- Output:
First Cyclop number > 10,000,000 is 111101111 at index 538084
- First prime Cyclop number > 10,000,000
<
let n=primeCyclops|>Seq.findIndex(fun n->n>10000000) in printfn "First prime Cyclop number > 10,000,000 is %d at index %d" (Seq.item n (primeCyclops)) n
- Output:
First prime Cyclop number > 10,000,000 is 111101129 at index 39319
- First blind Cyclop number > 10,000,000
<
let n=blindCyclops|>Seq.findIndex(fun n->n>10000000) in printfn "First blind Cyclop number > 10,000,000 is %d at index %d" (Seq.item n (blindCyclops)) n
- Output:
First blind Cyclop number > 10,000,000 is 111101161 at index 11393
- First palindromic prime Cyclop number > 10,000,000
<
let n=palindromicCyclops|>Seq.findIndex(fun n->n>10000000) in printfn "First palindromic prime Cyclop number > 10,000,000 is %d at index %d" (Seq.item n (palindromicCyclops)) n
- Output:
First palindromic prime Cyclop number > 10,000,000 is 114808411 at index 66
Factor
An attempt to make it fast has resulted in a lot of code, but here's the basic idea. Cyclops numbers are stored in four parts: an integer representing whatever is to the left of the 0
, a right integer, the number of digits in either side, and the maximum number with that many digits. For example, T{ cyclops f 33 34 2 99 }
represents the number 33034
. The left and right are zeroless; that is, they are not allowed to have any zeros.
In order to find the successor to a given cyclops number:
- Increment the right integer to the next zeroless integer using the following formula (from OEIS):
a(n+1) = f(a(n)) with f(x) = 1 + if x mod 10 < 9 then x else 10*f([x/10])
- Unless right = max, in which case increment the left integer instead in the same manner, and reset the right integer back to
max / 9
. - Unless left = max, in which case we move up to the next order of magnitude by adding
max/9 + 1
to both sides, adding1
to the number of digits, and setting the new max asmax := max * 10 + 9
Cyclops numbers in this form can be converted to integers (as late as possible) with int(c) = 10^(c.n + 1) * c.left + c.right
where n
is the number of digits in either side.
Cyclops numbers can be converted to blind form with the same formula without the + 1
.
<
USING: accessors formatting grouping io kernel lists lists.lazy
math math.functions math.primes prettyprint sequences
tools.memory.private tools.time ;
! ==========={[ Cyclops data type and operations ]}=============
TUPLE: cyclops left right n max ;
: <cyclops> ( -- cyclops ) 1 1 1 9 cyclops boa ;
: >cyclops< ( cyclops -- right left n )
[ right>> ] [ left>> ] [ n>> ] tri ;
M: cyclops >integer >cyclops< 1 + 10^ * + ;
: >blind ( cyclops -- n ) >cyclops< 10^ * + ;
: next-zeroless ( 9199 -- 9211 )
dup 10 mod 9 < [ 10 /i next-zeroless 10 * ] unless 1 + ;
: right++ ( cyclops -- cyclops' )
[ next-zeroless ] change-right ; inline
: left++ ( cyclops -- cyclops' )
[ next-zeroless ] change-left [ 9 /i ] change-right ;
: n++ ( cyclops -- cyclops' )
[ 1 + ] change-n [ 10 * 9 + ] change-max ;
: change-both ( cyclops quot -- cyclops' )
[ change-left ] keep change-right ; inline
: expand ( cyclops -- cyclops' )
dup max>> 9 /i 1 + '[ _ + ] change-both n++ ;
: carry ( cyclops -- cyclops' )
dup [ left>> ] [ max>> ] bi < [ left++ ] [ expand ] if ;
: succ ( cyclops -- next-cyclops )
dup [ right>> ] [ max>> ] bi < [ right++ ] [ carry ] if ;
! ============{[ List definitions & operations ]}===============
: lcyclops ( -- list ) <cyclops> [ succ ] lfrom-by ;
: lcyclops-int ( -- list ) lcyclops [ >integer ] lmap-lazy ;
: lprime-cyclops ( -- list )
lcyclops-int [ prime? ] lfilter ;
: lblind-prime-cyclops ( -- list )
lcyclops [ >integer prime? ] lfilter
[ >blind prime? ] lfilter ;
: reverse-digits ( 123 -- 321 )
0 swap [ 10 /mod rot 10 * + swap ] until-zero ;
: lpalindromic-prime-cyclops ( -- list )
lcyclops [ [ left>> ] [ right>> ] bi reverse-digits = ]
lfilter [ >integer prime? ] lfilter ;
: first>1e7 ( list -- elt index )
0 lfrom lzip [ first >integer 10,000,000 > ] lfilter car
first2 [ >integer ] dip [ commas ] bi@ ;
! ====================={[ OUTPUT ]}=============================
: first50 ( list -- )
50 swap ltake [ >integer ] lmap list>array 10 group
simple-table. ;
:: show ( desc list -- )
desc desc "First 50 %s numbers:\n" printf
list [ first50 nl ] [
first>1e7
"First %s number > 10,000,000: %s - at (zero based) index: %s.\n\n\n" printf
] bi ;
"cyclops" lcyclops-int show
"prime cyclops" lprime-cyclops show
"blind prime cyclops" lblind-prime-cyclops show
"palindromic prime cyclops" lpalindromic-prime-cyclops show
! Extra stretch?
"One billionth cyclops number:" print
999,999,999 lcyclops lnth >integer commas print
- Output:
First 50 cyclops numbers: 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 605 First cyclops number > 10,000,000: 111,101,111 - at (zero based) index: 538,083. First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First prime cyclops number > 10,000,000: 111,101,129 - at (zero based) index: 39,319. First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First blind prime cyclops number > 10,000,000: 111,101,161 - at (zero based) index: 11,393. First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First palindromic prime cyclops number > 10,000,000: 114,808,411 - at (zero based) index: 66. One billionth cyclops number: 35,296,098,111
FreeBASIC
Dim Shared As Double t
Dim Shared As Uinteger n(16), clops(52), primes(52), blinds(52), pals(52)
Dim Shared As Uinteger num, iClop, iPrime, iBlind, iPal
Dim Shared As Uinteger first1, last1, midPt
midPt = 8
Function convertToNumber As Uinteger
Dim As Uinteger p10 = 1, nr = 0, c2 = last1
Do
nr += n(c2) * p10
If c2 = midPt Then p10 *= 100 Else p10 *= 10 'Add 0 if at midPoint
c2 -= 1
Loop Until c2 < first1
Return nr
End Function
Sub increment(dgt As Uinteger)
If n(dgt) < 9 Then n(dgt) += 1 : Exit Sub
n(dgt) = 1
If dgt > first1 Then increment(dgt - 1) : Exit Sub 'Carry
first1 -= 1 : last1 += 1
For dgt = first1 To last1 'New width: set all digits to 1
n(dgt) = 1
Next dgt
End Sub
Function isPrime(v As Uinteger) As Boolean
'Skip check for 2 and 3 because we're starting at 101
If v Mod 2 = 0 Then Return False
If v Mod 3 = 0 Then Return False
Dim As Uinteger f = 5
While f*f <= v
If v Mod f = 0 Then Return False
f += 2
If v Mod f = 0 Then Return False
f += 4
Wend
Return True
End Function
Function isBlind As Boolean
Dim As Uinteger temp = 0
Swap temp, midPt 'Keep fn convertToNumber from adding 0 at midPt
Dim As Boolean rslt = isPrime(convertToNumber())
Swap temp, midPt
Return rslt
End Function
Function isPalindrome As Boolean
Dim As Uinteger a = first1, b = last1
While b > a
If n(a) <> n(b) Then Return False
a += 1 : b -= 1
Wend
Return True
End Function
Sub print50(title As String, addr() As Uinteger)
Dim As Uinteger r = 0
Print : Print title
While r < 50
Print Using "#########"; addr(r);
r += 1 : If r Mod 10 = 0 Then Print
Wend
End Sub
Sub display
print50(" First 50 Cyclopean numbers:", clops())
print50(" First 50 Cyclopean primes:", primes())
print50(" First 50 blind Cyclopean primes:", blinds())
print50(" First 50 palindromic Cyclopean primes:", pals())
Print
Print Using " First Cyclopean number above 10,000,000 is ######### at index ######"; clops(50); clops(51)
Print Using " First Cyclopean prime above 10,000,000 is ######### at index ######"; primes(50); primes(51)
Print Using " First blind Cyclopean prime above 10,000,000 is ######### at index ######"; blinds(50); blinds(51)
Print Using " First palindromic Cyclopean prime above 10,000,000 is ######### at index ######"; pals(50); pals(51)
Print
Print Using " Compute time: ###.### sec"; t
End Sub
Sub cyclops
clops(0) = 0 : iClop = 1 : iPrime = 0 : iBlind = 0 : iPal = 0
first1 = midPt-1 : last1 = midPt : n(first1) = 1 : n(last1) = 1
' Record first 50 numbers in each category
While iPal < 50
num = convertToNumber()
If iClop < 50 Then clops(iClop) = num
iClop += 1
If isPrime(num) Then
If iPrime < 50 Then primes(iPrime) = num : iPrime += 1
If iBlind < 50 Andalso isBlind() Then blinds(iBlind) = num : iBlind += 1
If iPal < 50 Andalso isPalindrome() Then pals(iPal) = num : iPal += 1
End If
num += 1
increment(last1)
Wend
' Keep counting Cyclops numbers until 10,000,000
While convertToNumber() < 1e7
increment(last1) : iClop += 1
Wend
' Find next number in each category
clops(50) = convertToNumber() : clops(51) = iClop
iPrime = 1 : iBlind = 1 : iPal = 1
While (iPrime Or iBlind Or iPal)
num = convertToNumber()
iClop += 1
If isPrime(num) Then
If iPrime = 1 Then primes(50) = num : primes(51) = iClop : iPrime = 0
If iBlind = 1 Andalso isBlind() Then blinds(50) = num : blinds(51) = iClop : iBlind = 0
If iPal = 1 Andalso isPalindrome() Then pals(50) = num : pals(51) = iClop : iPal = 0
End If
increment(last1)
Wend
End Sub
t = Timer
cyclops()
t = Timer - t
display()
Sleep
- Output:
First 50 Cyclopean numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First 50 Cyclopean primes: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First 50 blind Cyclopean primes: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First 50 palindromic Cyclopean primes: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First Cyclopean number above 10,000,000 is 111101111 at index 538084 First Cyclopean prime above 10,000,000 is 111101129 at index 538102 First blind Cyclopean prime above 10,000,000 is 111101161 at index 538130 First palindromic Cyclopean prime above 10,000,000 is 114808411 at index 766505 Compute time: 0.662 sec
FutureBasic
begin globals
CFTimeInterval t
long n(16), clops(52), primes(52), blinds(52), pals(52)
long num, iClop, iPrime, iBlind, iPal
short first1, last1, midPt
xref show(50) as long
midPt = 8
end globals
local fn convertToNumber as long
long p10 = 1, nr = 0
short c2 = last1
do
nr += n(c2) * p10
if c2 == midPt then p10 *= 100 else p10 *= 10 //Add 0 if at midPoint
c2--
until c2 < first1
end fn = nr
void local fn increment( dgt as short )
if n(dgt) < 9 then n(dgt)++ : exit fn
n(dgt) = 1
if dgt > first1 then fn increment( dgt - 1 ) : exit fn //Carry
first1-- : last1 ++
for dgt = first1 to last1 //New width: set all digits to 1
n(dgt) = 1
next
end fn
local fn isPrime( v as long ) as bool
//Skip check for 2 and 3 because we're starting at 101
if v mod 2 == 0 then exit fn = no
if v mod 3 == 0 then exit fn = no
long f = 5
while f*f <= v
if v mod f == 0 then exit fn = no
f += 2
if v mod f == 0 then exit fn = no
f += 4
wend
end fn = yes
local fn isBlind as bool
short temp = 0
swap temp, midPt //Keep fn convertToNumber from adding 0 at midPt
bool rslt = fn isPrime( fn convertToNumber )
swap temp, midPt
end fn = rslt
local fn isPalindrome as bool
short a = first1, b = last1
while b > a
if n(a) <> n(b) then exit fn = no
a++ : b--
wend
end fn = yes
void local fn print50( title as cfstringref, addr as ptr )
short r = 0
show = addr //Set array address to param
print : print title
while r < 50
printf @"%9ld\b",show(r)
r++ : if r mod 10 == 0 then print
wend
end fn
void local fn display
window 1, @"Cyclopean numbers", (0, 0, 680, 515 )
fn print50( @" First 50 Cyclopean numbers:", @clops( 0))
fn print50( @" First 50 Cyclopean primes:", @primes(0))
fn print50( @" First 50 blind Cyclopean primes:", @blinds(0))
fn print50( @" First 50 palindromic Cyclopean primes:",@pals( 0))
print
printf @" First Cyclopean number above 10,000,000 is %ld at index %ld", clops(50), clops(51)
printf @" First Cyclopean prime above 10,000,000 is %ld at index %ld", primes(50),primes(51)
printf @" First blind Cyclopean prime above 10,000,000 is %ld at index %ld", blinds(50),blinds(51)
printf @" First palindromic Cyclopean prime above 10,000,000 is %ld at index %ld", pals(50), pals(51)
print
printf @" Compute time: %.3f sec", t
end fn
void local fn cyclops
clops( 0 ) = 0 : iClop = 1 : iPrime = 0 : iBlind = 0 : iPal = 0
first1 = midPt-1 : last1 = midPt : n(first1) = 1 : n(last1) = 1
// Record first 50 numbers in each category
while ( iPal ) < 50
num = fn convertToNumber
if iClop < 50 then clops(iClop) = num
iClop++
if fn isPrime( num )
if iPrime < 50 then primes(iPrime) = num : iPrime++
if iBlind < 50 then if fn isBlind then blinds(iBlind) = num : iBlind++
if iPal < 50 then if fn isPalindrome then pals(iPal) = num : iPal++
end if
num++
fn increment( last1 )
wend
// Keep counting Cyclops numbers until 10,000,000
while fn convertToNumber < 10000000
fn increment( last1 ) : iClop++
wend
// Find next number in each category
clops(50) = fn convertToNumber : clops(51) = iClop
iPrime = 1 : iBlind = 1 : iPal = 1
while (iPrime or iBlind or iPal)
num = fn convertToNumber
iClop++
if fn isPrime( num )
if iPrime then primes(50) = num : primes(51) = iClop : iPrime--
if iBlind then if fn isBlind then blinds(50) = num : blinds(51) = iClop : iBlind--
if iPal then if fn isPalindrome then pals(50) = num : pals(51) = iClop : ipal--
end if
fn increment( last1 )
wend
end fn
t = fn CACurrentMediaTime
fn cyclops
t = fn CACurrentMediaTime - t
fn display
handleevents
- Output:
Go
package main
import (
"fmt"
"rcu"
"strconv"
"strings"
)
func findFirst(list []int) (int, int) {
for i, n := range list {
if n > 1e7 {
return n, i
}
}
return -1, -1
}
func reverse(s string) string {
chars := []rune(s)
for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
chars[i], chars[j] = chars[j], chars[i]
}
return string(chars)
}
func main() {
ranges := [][2]int{
{0, 0}, {101, 909}, {11011, 99099}, {1110111, 9990999}, {111101111, 119101111},
}
var cyclops []int
for _, r := range ranges {
numDigits := len(fmt.Sprint(r[0]))
center := numDigits / 2
for i := r[0]; i <= r[1]; i++ {
digits := rcu.Digits(i, 10)
if digits[center] == 0 {
count := 0
for _, d := range digits {
if d == 0 {
count++
}
}
if count == 1 {
cyclops = append(cyclops, i)
}
}
}
}
fmt.Println("The first 50 cyclops numbers are:")
for i, n := range cyclops[0:50] {
fmt.Printf("%6s ", rcu.Commatize(n))
if (i+1)%10 == 0 {
fmt.Println()
}
}
n, i := findFirst(cyclops)
ns, is := rcu.Commatize(n), rcu.Commatize(i)
fmt.Printf("\nFirst such number > 10 million is %s at zero-based index %s\n", ns, is)
var primes []int
for _, n := range cyclops {
if rcu.IsPrime(n) {
primes = append(primes, n)
}
}
fmt.Println("\n\nThe first 50 prime cyclops numbers are:")
for i, n := range primes[0:50] {
fmt.Printf("%6s ", rcu.Commatize(n))
if (i+1)%10 == 0 {
fmt.Println()
}
}
n, i = findFirst(primes)
ns, is = rcu.Commatize(n), rcu.Commatize(i)
fmt.Printf("\nFirst such number > 10 million is %s at zero-based index %s\n", ns, is)
var bpcyclops []int
var ppcyclops []int
for _, p := range primes {
ps := fmt.Sprint(p)
split := strings.Split(ps, "0")
noMiddle, _ := strconv.Atoi(split[0] + split[1])
if rcu.IsPrime(noMiddle) {
bpcyclops = append(bpcyclops, p)
}
if ps == reverse(ps) {
ppcyclops = append(ppcyclops, p)
}
}
fmt.Println("\n\nThe first 50 blind prime cyclops numbers are:")
for i, n := range bpcyclops[0:50] {
fmt.Printf("%6s ", rcu.Commatize(n))
if (i+1)%10 == 0 {
fmt.Println()
}
}
n, i = findFirst(bpcyclops)
ns, is = rcu.Commatize(n), rcu.Commatize(i)
fmt.Printf("\nFirst such number > 10 million is %s at zero-based index %s\n", ns, is)
fmt.Println("\n\nThe first 50 palindromic prime cyclops numbers are:\n")
for i, n := range ppcyclops[0:50] {
fmt.Printf("%9s ", rcu.Commatize(n))
if (i+1)%8 == 0 {
fmt.Println()
}
}
n, i = findFirst(ppcyclops)
ns, is = rcu.Commatize(n), rcu.Commatize(i)
fmt.Printf("\n\nFirst such number > 10 million is %s at zero-based index %s\n", ns, is)
}
- Output:
The first 50 cyclops numbers are: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First such number > 10 million is 111,101,111 at zero-based index 538,084 The first 50 prime cyclops numbers are: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11,027 11,047 11,057 11,059 11,069 11,071 11,083 11,087 11,093 12,011 12,037 12,041 12,043 12,049 12,071 12,073 12,097 13,033 13,037 13,043 13,049 13,063 13,093 13,099 14,011 14,029 14,033 14,051 14,057 14,071 14,081 14,083 14,087 15,013 15,017 First such number > 10 million is 111,101,129 at zero-based index 39,319 The first 50 blind prime cyclops numbers are: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11,071 11,087 11,093 12,037 12,049 12,097 13,099 14,029 14,033 14,051 14,071 14,081 14,083 14,087 15,031 15,053 15,083 16,057 16,063 16,067 16,069 16,097 17,021 17,033 17,041 17,047 17,053 17,077 18,047 18,061 18,077 18,089 19,013 19,031 19,051 19,073 First such number > 10 million is 111,101,161 at zero-based index 11,393 The first 50 palindromic prime cyclops numbers are: 101 16,061 31,013 35,053 38,083 73,037 74,047 91,019 94,049 1,120,211 1,150,511 1,160,611 1,180,811 1,190,911 1,250,521 1,280,821 1,360,631 1,390,931 1,490,941 1,520,251 1,550,551 1,580,851 1,630,361 1,640,461 1,660,661 1,670,761 1,730,371 1,820,281 1,880,881 1,930,391 1,970,791 3,140,413 3,160,613 3,260,623 3,310,133 3,380,833 3,460,643 3,470,743 3,590,953 3,670,763 3,680,863 3,970,793 7,190,917 7,250,527 7,310,137 7,540,457 7,630,367 7,690,967 7,750,577 7,820,287 First such number > 10 million is 114,808,411 at zero-based index 66
Haskell
<
import Control.Monad (replicateM)
import Data.Numbers.Primes (isPrime)
--------------------- CYCLOPS NUMBERS --------------------
cyclops :: [Integer]
cyclops = [0 ..] >>= go
where
go 0 = [0]
go n =
(\s -> read s :: Integer)
<$> (fmap ((<>) . (<> "0")) >>= (<*>))
(replicateM n ['1' .. '9'])
blindPrime :: Integer -> Bool
blindPrime n =
let s = show n
m = quot (length s) 2
in isPrime $
(\t -> read t :: Integer)
(take m s <> drop (succ m) s)
palindromic :: Integer -> Bool
palindromic = ((==) =<< reverse) . show
-------------------------- TESTS -------------------------
main :: IO ()
main =
(putStrLn . unlines)
[ "First 50 Cyclops numbers – A134808:",
unwords (show <$> take 50 cyclops),
"",
"First 50 Cyclops primes – A134809:",
unwords $ take 50 [show n | n <- cyclops, isPrime n],
"",
"First 50 blind prime Cyclops numbers – A329737:",
unwords $
take
50
[show n | n <- cyclops, isPrime n, blindPrime n],
"",
"First 50 prime palindromic cyclops numbers – A136098:",
unwords $
take
50
[show n | n <- cyclops, isPrime n, palindromic n]
]
First 50 Cyclops numbers – A134808: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First 50 Cyclops primes – A134809: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First 50 blind prime Cyclops numbers – A329737: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First 50 prime palindromic cyclops numbers – A136098: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
J
<
makecyclops =: 3 : 0
NB. y Number of digits (must be odd, greater than 2)
side =. nums #~ -. '0' +./@:="1 nums =: ":"0 (0.1 * base) }. i. base =. 10 ^ <. -: y
, /:~ ". ,/ (side ,"1 0 '0') ,"1"2 1 side
)
go =: 3 : 0
cyclops =. 0 , ; <@:makecyclops"0 (3 5 7)
('First 50 Cyclops numbers:' , ": 5 10 $ cyclops) (1!:2) 2
((LF , 'First 50 prime Cyclops numbers:') , ": 5 10 $ primes =. cyclops ([ #~ e.) p: i.600000) (1!:2) 2
((LF , 'First 50 blind prime Cyclops numbers:') , ": 5 10 $ (primes #~ (, ". '0' -.~"0 1 ": ,. primes) e. p: i.10000)) (1!:2) 2
(LF , 'First 50 palindromic prime Cyclops numbers:') , ": 5 10 $ primes #~ > (-: |.) &. > <@:":"1 ,. primes
)
go '' First 50 Cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First 50 prime Cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First 50 blind prime Cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First 50 palindromic prime Cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
Java
import java.util.ArrayList;
import java.util.List;
public final class CyclopsNumbers {
public static void main(String[] args) {
List<Integer> cyclops = new ArrayList<Integer>();
List<Integer> primeCyclops = new ArrayList<Integer>();
List<Integer> blindPrimeCyclops = new ArrayList<Integer>();
List<Integer> palindromicPrimeCyclops = new ArrayList<Integer>();
List<List<Integer>> ranges = List.of( List.of( 0, 0), List.of( 101, 909),
List.of( 11011, 99099 ), List.of( 1110111, 9990999 ), List.of( 111101111, 119101111 ) );
for ( List<Integer> range : ranges) {
for ( int i = range.getFirst(); i <= range.getLast(); i++ ) {
String value = String.valueOf(i);
if ( isCyclopsNumber(value) ) {
cyclops.addLast(i);
if ( isPrime(i) ) {
primeCyclops.addLast(i);
if ( isBlind(value) ) {
blindPrimeCyclops.addLast(i);
}
if ( isPalindromic(value) ) {
palindromicPrimeCyclops.addLast(i);
}
}
}
}
}
System.out.println("The first 50 Cyclops numbers:");
for ( int i = 0; i < 50; i++ ) {
System.out.print(String.format("%6s%s", cyclops.get(i), ( i % 10 == 9 ? "\n" : "" )));
}
System.out.println();
int firstIndex = firstIndex(cyclops);
System.out.println("The first cyclops number greater than ten million is "
+ cyclops.get(firstIndex) + " at zero based index " + firstIndex);
System.out.println();
System.out.println("The first 50 prime Cyclops numbers:");
for ( int i = 0; i < 50; i++ ) {
System.out.print(String.format("%7s%s", primeCyclops.get(i), ( i % 10 == 9 ? "\n" : "" )));
}
System.out.println();
firstIndex = firstIndex(primeCyclops);
System.out.println("The first prime cyclops number greater than ten million is "
+ primeCyclops.get(firstIndex) + " at zero based index " + firstIndex);
System.out.println();
System.out.println("The first 50 blind prime Cyclops numbers:");
for ( int i = 0; i < 50; i++ ) {
System.out.print(String.format("%7s%s", blindPrimeCyclops.get(i), ( i % 10 == 9 ? "\n" : "" )));
}
System.out.println();
firstIndex = firstIndex(blindPrimeCyclops);
System.out.println("The first blind prime cyclops number greater than ten million is "
+ blindPrimeCyclops.get(firstIndex) + " at zero based index " + firstIndex);
System.out.println();
System.out.println("The first 50 palindromic prime Cyclops numbers:");
for ( int i = 0; i < 50; i++ ) {
System.out.print(
String.format("%9s%s", palindromicPrimeCyclops.get(i), ( i % 10 == 9 ? "\n" : "" )));
}
System.out.println();
firstIndex = firstIndex(palindromicPrimeCyclops);
System.out.println("The first palindromic prime cyclops number greater than ten million is "
+ palindromicPrimeCyclops.get(firstIndex) + " at zero based index " + firstIndex);
System.out.println();
}
private static int firstIndex(List<Integer> numbers) {
int start = 0;
int end = numbers.size() - 1;
while ( start <= end ) {
final int mid = start + ( end - start ) / 2;
if ( numbers.get(mid) <= 10_000_000 ) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return start;
}
private static boolean isCyclopsNumber(String text) {
return text.charAt(text.length() / 2) == '0' && text.indexOf('0') == text.lastIndexOf('0');
}
private static boolean isPalindromic(String text) {
for ( int i = 0; i < text.length() / 2; i++ ) {
if ( text.charAt(i) != text.charAt(text.length() - 1 - i) ) {
return false;
}
}
return true;
}
private static boolean isBlind(String text) {
final int middle = text.length() / 2;
final String withoutZero = text.substring(0, middle) + text.substring(middle + 1);
return isPrime(Integer.valueOf(withoutZero));
}
private static boolean isPrime(int number) {
if ( number < 2 ) {
return false;
}
if ( number % 2 == 0 ) {
return number == 2;
}
if ( number % 3 == 0 ) {
return number == 3;
}
int k = 5;
while ( k * k <= number ) {
if ( number % k == 0 ) {
return false;
}
k += 2;
if ( number % k == 0 ) {
return false;
}
k += 4;
}
return true;
}
}
The first 50 Cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 The first cyclops number greater than ten million is 111101111 at zero based index 538084 The first 50 prime Cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 The first prime cyclops number greater than ten million is 111101129 at zero based index 39319 The first 50 blind prime Cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 The first blind prime cyclops number greater than ten million is 111101161 at zero based index 11393 The first 50 palindromic prime Cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 The first palindromic prime cyclops number greater than ten million is 114808411 at zero based index 66
jq
Note that the indices shown in the output are 0-based, as jq's index origin is 0.
<
## Generic helper functions
def count(s): reduce s as $x (0; .+1);
# counting from 0
def enumerate(s): foreach s as $x (-1; .+1; [., $x]);
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
## Prime numbers
def is_prime:
if . == 2 then true
else
2 < . and . % 2 == 1 and
(. as $in
| (($in + 1) | sqrt) as $m
| [false, 3] | until( .[0] or .[1] > $m; [$in % .[1] == 0, .[1] + 2])
| .[0]
| not)
end ;
## Cyclops numbers
def iscyclops:
(tostring | explode) as $d
| ($d|length) as $l
| (($l + 1) / 2 - 1) as $m
| ($l % 2 == 1) and $d[$m] == 48 and count($d[] | select(.== 48)) == 1 # "0"
;
# Generate a stream of cyclops numbers, in increasing numeric order, from 0
def cyclops:
# generate a stream of cyclops numbers with $n digits on each side of the central 0
def w:
if . == 0 then ""
else (.-1)|w as $left
| $left + (range(1;10)|tostring)
end;
def c: w as $left | $left + "0" + w;
range(0; infinite) | c | tonumber;
# Generate a stream of palindromic cyclops numbers, in increasing numeric order, from 0
def palindromiccyclops:
def r: explode|reverse|implode;
def c: . as $n
| if $n == 0 then "0"
elif $n == 1
then (range(1;10)|tostring) as $base
| $base + "0" + ($base | r)
else (range(pow(10;$n-1); pow(10; $n))|tostring|select(test("0")|not)) as $base
| $base + "0" + ($base | r)
end;
range(0; infinite) | c | tonumber;
# check that a cyclops number minus the 0 is prime
def cyclops_isblind:
(tostring | explode) as $d
| ($d|length) as $l
| (($l + 1) / 2 - 1) as $m
| ((( $d[:$m] + $d[$m+1:] ) | implode | tonumber) | is_prime);
# check that a cyclops number is a palindrome
def cyclops_ispalindromic:
. as $in
| (tostring | explode) as $d
| ($d|length) as $l
| (($l + 1) / 2 - 1) as $m
| $d[:$m] == ( $d[$m+1:] | reverse) ;
The tasks
<
def print5x10($width):
. as $a
| range(0;5) as $i
| reduce range(0;10) as $j (""; . + ($a[10*$i + $j] | lpad($width)));
def main_task($n):
"The first \($n) cyclops numbers are:",
([limit($n; cyclops)] | print5x10(7)),
"\nThe first \($n) prime cyclops numbers are:",
([limit($n; cyclops | select(is_prime))] | print5x10(7)),
"\nThe first \($n) blind prime cyclops numbers are:",
([limit($n; cyclops | select(is_prime and cyclops_isblind))] | print5x10(7)),
"\nThe first \($n) palindromic prime cyclops numbers are:",
([limit($n; cyclops | select(cyclops_ispalindromic and is_prime))] | print5x10(8)) ;
def stretch_task($big):
def pp: " \(.[1]) at index \(.[0]).";
"\nFirst cyclops greater than \($big):" +
(first(enumerate(cyclops) | select(.[1] > $big))|pp),
"\nThe next prime cyclops number after \($big):" +
(first(enumerate(cyclops | select(is_prime)) | select(.[1] | (. > $big)) ) | pp),
"\nThe first blind prime cyclops number greater than \($big):" +
(first(enumerate(cyclops | select(is_prime and cyclops_isblind)) | select(.[1] > $big) )|pp),
"\nThe first palindromic prime cyclops number greater than \($big):" +
(first(enumerate(palindromiccyclops | select(is_prime)) | select(.[1] > $big) ) | pp) ;
main_task(50),
stretch_task(pow(10;7))
- Output:
The first 50 cyclops numbers are: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 The first 50 prime cyclops numbers are: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 The first 50 blind prime cyclops numbers are: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 The first 50 palindromic prime cyclops numbers are: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First cyclops greater than 10000000: 111101111 at index 538084. The next prime cyclops number after 10000000: 111101129 at index 39319. The first blind prime cyclops number greater than 10000000: 111101161 at index 11393. The first palindromic prime cyclops number greater than 10000000: 114808411 at index 66.
Julia
Julia's indexes are 1 based, hence one greater than those of 0 based indices.
<
print5x10(a, w = 8) = for i in 0:4, j in 1:10 print(lpad(a[10i + j], w), j == 10 ? "\n" : "") end
function iscyclops(n)
d = digits(n)
l = length(d)
return isodd(l) && d[l ÷ 2 + 1] == 0 && count(x -> x == 0, d) == 1
end
function isblindprimecyclops(n)
d = digits(n)
l = length(d)
m = l ÷ 2 + 1
(n == 0 || iseven(l) || d[m] != 0 || count(x -> x == 0, d) != 1) && return false
return isprime(evalpoly(10, [d[1:m-1]; d[m+1:end]]))
end
function ispalindromicprimecyclops(n)
d = digits(n)
l = length(d)
return n > 0 && isodd(l) && d[l ÷ 2 + 1] == 0 && count(x -> x == 0, d) == 1 && d == reverse(d)
end
function findcyclops(N, iscycs, nextcandidate)
i, list = nextcandidate(-1), Int[]
while length(list) < N
iscycs(i) && push!(list, i)
i = nextcandidate(i)
end
return list
end
function nthcyclopsfirstafter(lowerlimit, iscycs, nextcandidate)
i, found = 0, 0
while true
if iscycs(i)
found += 1
i >= lowerlimit && break
end
i = nextcandidate(i)
end
return i, found
end
function testcyclops()
println("The first 50 cyclops numbers are:")
print5x10(findcyclops(50, iscyclops, x -> x + 1))
n, c = nthcyclopsfirstafter(10000000, iscyclops, x -> x + 1)
println("\nThe next cyclops number after 10,000,000 is $n at position $c.")
println("\nThe first 50 prime cyclops numbers are:")
print5x10(findcyclops(50, iscyclops, x -> nextprime(x + 1)))
n, c = nthcyclopsfirstafter(10000000, iscyclops, x -> nextprime(x + 1))
println("\nThe next prime cyclops number after 10,000,000 is $n at position $c.")
println("\nThe first 50 blind prime cyclops numbers are:")
print5x10(findcyclops(50, isblindprimecyclops, x -> nextprime(x + 1)))
n, c = nthcyclopsfirstafter(10000000, isblindprimecyclops, x -> nextprime(x + 1))
println("\nThe next prime cyclops number after 10,000,000 is $n at position $c.")
println("\nThe first 50 palindromic prime cyclops numbers are:")
print5x10(findcyclops(50, ispalindromicprimecyclops, x -> nextprime(x + 1)))
n, c = nthcyclopsfirstafter(10000000, ispalindromicprimecyclops, x -> nextprime(x + 1))
println("\nThe next prime cyclops number after 10,000,000 is $n at position $c.")
end
testcyclops()
- Output:
The first 50 cyclops numbers are: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 The next cyclops number after 10,000,000 is 111101111 at position 538085. The first 50 prime cyclops numbers are: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 The next prime cyclops number after 10,000,000 is 111101129 at position 39321. The first 50 blind prime cyclops numbers are: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 The next prime cyclops number after 10,000,000 is 111101161 at position 11394. The first 50 palindromic prime cyclops numbers are: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 The next prime cyclops number after 10,000,000 is 114808411 at position 67.
Ksh
<
#!/bin/ksh
# Cyclops numbers (odd number of digits that has a zero in the center)
# - first 50 cyclops numbers
# - first 50 prime cyclops numbers
# - first 50 blind prime cyclops numbers
# - first 50 palindromic prime cyclops numbers
# # Variables:
#
integer MAXN=50
# # Functions:
#
# # Function _isprime(n) return 1 for prime, 0 for not prime
#
function _isprime {
typeset _n ; integer _n=$1
typeset _i ; integer _i
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
}
# # Function _iscyclops(n) - return 1 for cyclops number
#
function _iscyclops {
typeset _n ; integer _n=$1
(( ! ${#_n}&1 )) && return 0 # must have odd number of digits
(( ${_n:$((${#_n}/2)):1} )) && return 0 # must have center zero
[[ $(_blind ${_n}) == *0* ]] && return 0 # No other zeros
return 1
}
# # Function _blind(n) - return a "blinded" cyclops number
#
function _blind {
typeset _n ; _n="$1"
echo "${_n:0:$((${#_n}/2))}${_n:$((${#_n}/2+1)):$((${#_n}/2))}"
}
# # Function _ispalindrome(n) - return 1 for palindromic number
#
function _ispalindrome {
typeset _n ; _n="$1"
typeset _flippedn
_flippedn=$(_flipit ${_n:$((${#_n}/2+1)):$((${#_n}/2))})
[[ ${_n:0:$((${#_n}/2))} != ${_flippedn} ]] && return 0
return 1
}
# # Function _flipit(string) - return flipped string
#
function _flipit {
typeset _buf ; _buf="$1"
typeset _tmp ; unset _tmp
for (( _i=$(( ${#_buf}-1 )); _i>=0; _i-- )); do
_tmp="${_tmp}${_buf:${_i}:1}"
done
echo "${_tmp}"
}
######
# main #
######
integer cy=prcy=blprcy=palprcy=0 # counters
typeset -a cyarr prcyarr blprcyarr palprcyarr
for i in {101..909} {11011..99099} {1110111..9990999}; do
_iscyclops ${i} ; (( ! $? )) && continue
(( ++cy <= MAXN )) && cyarr+=( ${i} )
_isprime ${i} ; (( ! $? )) && continue
(( ++prcy <= MAXN )) && prcyarr+=( ${i} )
if (( blprcy < MAXN )); then
_isprime $(_blind ${i})
(( $? )) && { (( blprcy++ )) ; blprcyarr+=( ${i} ) }
fi
if (( palprcy < MAXN )); then
_ispalindrome ${i}
(( $? )) && { (( palprcy++ )) ; palprcyarr+=( ${i} ) }
fi
(( palprcy >= MAXN && blprcy >= MAXN && prcy >= MAXN && cy >= MAXN )) && break
done
print "First $MAXN cyclops numbers:"
print ${cyarr[@]}
print "\nFirst $MAXN prime cyclops numbers:"
print ${prcyarr[@]}
print "\nFirst $MAXN blind prime cyclops numbers:"
print ${blprcyarr[@]}
print "\nFirst $MAXN palindromic prime cyclops numbers:"
print ${palprcyarr[@]}
- Output:
First 50 cyclops numbers: 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 605
First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017
First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073
First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
Mathematica / Wolfram Language
<
a = Flatten[Table[FromDigits[{i, 0, j}], {i, 9}, {j, 9}], 1];
b = Flatten@Table[FromDigits@{i, j}, {i, 9}, {j, 9}];
c = Flatten@Table[FromDigits@{i, j, k}, {i, 9}, {j, 9}, {k, 9}];
blindPrimeQ[n_] :=
Block[{digits = IntegerDigits@n, m},
m = Floor[Length[digits]/2];
PrimeQ[FromDigits@Join[digits[[;; m]], digits[[-m ;;]]]]]
palindromeQ[n_] :=
Block[{digits = IntegerDigits@n}, digits === Reverse[digits]]
cyclopsQ[n_] :=
Block[{digits = IntegerDigits@n, len, ctr},
len = Length[digits];
ctr = Ceiling[len/2];
And @@ {Mod[len, 2] == 1, ctr > 0, digits[[ctr]] == 0,
FreeQ[Drop[digits, {ctr}], 0]}]
cyclops = (* all Cyclops numbers with 3, 5 or 7 digits *)
Flatten@{a,
Outer[
FromDigits@Flatten@{IntegerDigits@#1, 0, IntegerDigits@#2} &, b,
b],
Outer[
FromDigits@Flatten@{IntegerDigits@#1, 0, IntegerDigits@#2} &, c,
c]};
x = NestWhile[NextPrime, 10^8, ! (cyclopsQ@# && PrimeQ@#) &];
Labeled[Partition[Flatten[{0, a}][[;; 50]], 10] //
TableForm, "First 50 Cyclop numbers", Top]
Labeled[Partition[(primeCyclops = Cases[cyclops, _?PrimeQ])[[;; 50]],
10] // TableForm, "First 50 prime cyclops numbers", Top]
Labeled[Partition[(blind = Cases[primeCyclops, _?blindPrimeQ])[[;;
50]], 10] //
TableForm, "First 50 blind prime cyclops numbers", Top]
Labeled[Partition[(p = Cases[primeCyclops, _?palindromeQ])[[;; 50]],
10] // TableForm, "First 50 palindromic prime Cyclops Numbers", Top]
Labeled[TableForm[{{x,
Length@primeCyclops}, {NestWhile[NextPrime,
x + 1, ! (cyclopsQ@# && PrimeQ@# && blindPrimeQ@#) &],
Length@blind}, {NestWhile[NextPrime,
x + 1, ! (cyclopsQ@# && PrimeQ@# && palindromeQ@#) &],
Length@p}},
TableHeadings -> {{"Prime", "Blind Prime",
"Palindromic Prime"}, {"Value",
"Index"}}], "First Cyclops numeber > 10,000,000", Top]
- Output:
First 50 Cyclop numbers 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604
First 50 prime cyclops numbers 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017
First 50 blind prime cyclops numbers 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073
First 50 palindromic prime Cyclops Numbers 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
First Cyclops numbers > 10,000,000
Value IndexPrime 111101129 39319 Blind Prime 111101161 11393 Palindromic Prime 114808411 66
Nim
<
import strutils, times
const Ranges = [0..0, 101..909, 11011..99099, 1110111..9990999, 111101111..999909999]
func isCyclops(d: string): bool =
d[d.len shr 1] == '0' and d.count('0') == 1
func isPrime(n: Natural): bool =
if n < 2: return
if n mod 2 == 0: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
if n mod d == 0: return false
inc d, 4
return true
func isBlind(d: string): bool =
var d = d
let m = d.len shr 1
result = (d[0..m-1] & d[m+1..^1]).parseInt().isPrime
func isPalindromic(d: string): bool =
for i in 1..d.len:
if d[i-1] != d[^i]: return
result = true
iterator cyclops(): (int, int) =
var count = 0
for r in Ranges:
for n in r:
if ($n).isCyclops:
inc count
yield (count, n)
iterator primeCyclops(): (int, int) =
var count = 0
for (_, n) in cyclops():
if n.isPrime:
inc count
yield (count, n)
iterator blindPrimeCyclops(): (int, int) =
var count = 0
for (_, n) in primeCyclops():
if ($n).isBlind:
inc count
yield (count, n)
iterator palindromicPrimeCyclops(): (int, int) =
var count = 0
for r in Ranges:
for n in r:
let d = $n
if d.isCyclops and d.isPalindromic and n.isPrime:
inc count
yield (count, n)
let t0 = cpuTime()
echo "List of first 50 cyclops numbers:"
for i, n in cyclops():
stdout.write ($n).align(3), if i mod 10 == 0: '\n' else: ' '
if i == 50: break
echo "\nList of first 50 prime cyclops numbers:"
for i, n in primeCyclops():
stdout.write ($n).align(5), if i mod 10 == 0: '\n' else: ' '
if i == 50: break
echo "\nList of first 50 blind prime cyclops numbers:"
for i, n in blindPrimeCyclops():
stdout.write ($n).align(5), if i mod 10 == 0: '\n' else: ' '
if i == 50: break
echo "\nList of first 50 palindromic prime cyclops numbers:"
for i, n in palindromicPrimeCyclops():
stdout.write ($n).align(7), if i mod 10 == 0: '\n' else: ' '
if i == 50: break
for i, n in cyclops():
if n > 10_000_000:
echo "\nFirst cyclops number greater than ten million is ",
($n).insertSep(), " at 1-based index: ", i
break
for i, n in primeCyclops():
if n > 10_000_000:
echo "\nFirst prime cyclops number greater than ten million is ",
($n).insertSep(), " at 1-based index: ", i
break
for i, n in blindPrimeCyclops():
if n > 10_000_000:
echo "\nFirst blind prime cyclops number greater than ten million is ",
($n).insertSep(), " at 1-based index: ", i
break
for i, n in palindromicPrimeCyclops():
if n > 10_000_000:
echo "\nFirst palindromic prime cyclops number greater than ten million is ",
($n).insertSep(), " at 1-based index: ", i
break
echo "\nExecution time: ", (cpuTime() - t0).formatFloat(ffDecimal, precision = 3), " seconds."
- Output:
List of first 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 List of first 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 List of first 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 List of first 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First cyclops number greater than ten million is 111_101_111 at 1-based index: 538085 First prime cyclops number greater than ten million is 111_101_129 at 1-based index: 39320 First blind prime cyclops number greater than ten million is 111_101_161 at 1-based index: 11394 First palindromic prime cyclops number greater than ten million is 114_808_411 at 1-based index: 67 Execution time: 2.936 seconds.
Pascal
Using left and reight part of cyclops number in base 9 added to every digit 1 [0..8]->[1..9]
Simple trial division for isprime is very slow.
Try to find one billionth like Cyclops_numbers#Factor
Added conversion of zero-based index to cyclops number
verified List of palindromic prime cyclops numbers
<
program cyclops;
{$IFDEF FPC}
{$MODE DELPHI}
{$OPTIMIZATION ON,ALL}
{$CodeAlign proc=32,loop=1}
{$ENDIF}
//extra https://oeis.org/A136098/b136098.txt take ~37 s( TIO.RUN )
uses
sysutils;
const
BIGLIMIT = 10*1000*1000;
type
//number in base 9
tnumdgts = array[0..10] of byte;
tpnumdgts = pByte;
tnum9 = packed record
nmdgts : tnumdgts;
nmMaxDgtIdx :byte;
nmNum : uint32;
end;
tCN = record
cnRight,
cnLeft : tNum9;
cnNum : Uint64;
cndigits,
cnIdx : Uint32;
end;
tCyclopsList = array of Uint64;
procedure InitCycNum(var cn:tCN);forward;
var
cnMin,cnPow10Shift,cnPow9 : array[0..15] of Uint64;
Cyclops :tCyclopsList;
function IndexToCyclops(n:Uint64):tCN;
//zero-based index
var
dgtCnt,i,num : UInt32;
q,p9: Uint64;
Begin
InitCycNum(result);
if n = 0 then
EXIT;
result.cnIdx := n;
dgtCnt := 0;
repeat
p9 := sqr(cnPow9[dgtCnt]);
if n < p9 then
break;
n -= p9;
inc(dgtCnt)
until dgtcnt>10;
dec(dgtCnt);
with result do
Begin
with cnRight do
Begin
nmMaxDgtIdx := dgtCnt;
For i := 0 to dgtCnt do
begin
q := n DIV 9;
nmdgts[i] := n-9*q;
n := q;
end;
num :=0;
For i := dgtcnt downto 0 do
num := num*10+nmdgts[i]+1;
nmNum:= num;
cnNum := num;
end;
with cnLeft do
Begin
nmMaxDgtIdx := dgtCnt;
For i := 0 to dgtCnt do
begin
q := n DIV 9;
nmdgts[i] := n-9*q;
n := q;
end;
num :=0;
For i := dgtcnt downto 0 do
num := num*10+nmdgts[i]+1;
nmNum:= num;
cnNum += num*cnPow10Shift[dgtCnt];
cndigits := dgtCnt;
end;
end;
end;
procedure Out_Cyclops(const cl:tCyclopsList;colw,colc:NativeInt);
var
i,n : NativeInt;
Begin
n := High(cl);
If n > 100 then
n := 100;
For i := 0 to n do
begin
write(cl[i]:colw);
if (i+1) mod colc = 0 then
writeln;
end;
if (i+1) mod colc <> 0 then
writeln;
if n< High(cl) then
writeln(High(cl)+1,' : ',cl[High(cl)]);
end;
procedure InitCnMinPow;
//min = (0,1,11,111,1111,11111,111111,1111111,11111111,...);
var
i,min,pow,pow9 : Uint64;
begin
min := 0;
pow := 100;
pow9 := 1;
For i :=0 to High(cnMin) do
begin
cnMin[i] := min;
min := 10*min+1;
cnPow10Shift[i] := pow;
pow *= 10;
cnPow9[i] := pow9;
pow9 *= 9;
end;
end;
procedure ClearNum9(var tn:tNum9;idx:Uint32);
begin
fillchar(tn,SizeOf(tn),#0);
tn.nmNum := cnMin[idx+1];
end;
Procedure InitCycNum(var cn:tCN);
Begin
with cn do
Begin
cndigits := 0;
ClearNum9(cnLeft,0);
ClearNum9(cnRight,0);
cnNum := 0;
cnIdx := 0;
end;
end;
procedure IncNum9(var tn:tNum9);
var
idx,fac,n: Uint32;
begin
idx := 0;
with tn do
Begin
fac := 1;
n := nmdgts[0]+1;
inc(nmNum);
repeat
if n < 9 then
break;
inc(nmNum,fac);
nmdgts[idx] :=0;
inc(idx);
fac *= 10;
n := nmdgts[idx]+1;
until idx > nmMaxDgtIdx;
If idx > High(nmdgts) then
EXIT;
nmdgts[idx] := n;
if nmMaxDgtIdx<Idx then
nmMaxDgtIdx := Idx;
end;
end;
procedure NextCycNum(var cycnum:tCN);
begin
with cycnum do
Begin
if cnIdx <> 0 then
begin
//increment right digits
IncNum9(cnRight);
if cnRight.nmMaxDgtIdx > cndigits then
Begin
//set right digits to minimum
ClearNum9(cnRight,cndigits);
//increment left digits
IncNum9(cnLeft);
//One more digit ?
if cnLeft.nmMaxDgtIdx > cndigits then
Begin
inc(cndigits);
ClearNum9(cnLeft,cndigits);
ClearNum9(cnRight,cndigits);
if cndigits>High(tnumdgts) then
cndigits := High(tnumdgts);
end;
end;
cnNum := cnLeft.nmNum*cnPow10Shift[cndigits]+cnRight.nmNUm;
inc(cnIdx);
end
else
Begin
cnNum := 101;
cnIdx := 1;
end;
end;
end;
procedure MakePalinCycNum(var cycnum:tCN);
//make right to be palin of left
var
n,dgt : Uint32;
i,j:NativeInt;
Begin
n := 0;
with cycnum do
Begin
i := 0;
For j := cnDigits downto 0 do
begin
dgt := cnLeft.nmdgts[i];
cnRight.nmdgts[j] := dgt;
n := 10*n+(dgt+1);
inc(i);
end;
cnRight.nmNum := n;
cnNum := cnLeft.nmNum*cnPow10Shift[cndigits]+n;
end;
end;
procedure IncLeftCn(var cn:tCN);
Begin
with cn do
Begin
//set right digits to minimum
ClearNum9(cnRight,cndigits);
//increment left digits
IncNum9(cnLeft);
//One more digit ?
if cnLeft.nmMaxDgtIdx > cndigits then
Begin
inc(cndigits);
ClearNum9(cnLeft,cndigits);
ClearNum9(cnRight,cndigits);
if cndigits>High(tnumdgts) then
cndigits := High(tnumdgts);
end;
cnNum := cnLeft.nmNum*cnPow10Shift[cndigits]+cnRight.nmNUm;
end;
end;
function isPalinCycNum(const cycnum:tCN):boolean;
var
i,j:NativeInt;
Begin
with cycnum do
Begin
i := cnDigits;
j := 0;
repeat
result := (cnRight.nmdgts[i]=cnLeft.nmdgts[j]);
if not(result) then
BREAK;
dec(i);
inc(j);
until i<0;
end;
end;
function FirstCyclops(cnt:NativeInt):tCN;
var
i: NativeInt;
begin
setlength(Cyclops,cnt);
i := 0;
InitCycNum(result);
while i < cnt do
begin
Cyclops[i] := result.cnNum;
inc(i);
NextCycNum(result);
end;
repeat
NextCycNum(result);
inc(i);
until result.cnNum> BIGLIMIT;
end;
function isPrime(n:Uint64):boolean;
var
p,q : Uint64;
Begin
{
if n< 4 then
Begin
if n < 2 then
EXIT(false);
EXIT(true);
end;
if n = 5 then
exit(true);}
if (n AND 1 = 0) then
EXIT(false);
q := n div 3;
if n - 3*q = 0 then
EXIT(false);
p := 5;
{$CodeAlign loop=1}
repeat
q := n div p;
if n-q*p = 0 then
EXIT(false);
p += 2;
q := n div p;
if n-q*p = 0 then
EXIT(false);
if q < p then
break;
p += 4;
until false;
EXIT(true);
end;
function FirstPrimeCyclops(cnt:NativeInt):tCN;
var
i: NativeInt;
begin
i := 0;
setlength(Cyclops,cnt);
InitCycNum(result);
while i<cnt do
begin
if isPrime(result.cnNum) then
Begin
Cyclops[i] := result.cnNum;
inc(i);
end;
NextCycNum(result);
end;
repeat
if isPrime(result.cnNum) then
begin;
inc(i);
if result.cnNum > BIGLIMIT then
BREAK;
end;
NextCycNum(result);
until false;
result.cnIdx := i;
end;
function FirstBlindPrimeCyclops(cnt:NativeInt):tCN;
var
n: Uint64;
i: NativeInt;
isPr:Boolean;
begin
i := 0;
setlength(Cyclops,cnt);
InitCycNum(result);
while i< cnt do
begin
with result do
if isPrime(cnNum) then
Begin
n:= cnRight.nmNum;
if cndigits > 0 then
n += cnLeft.nmNum*cnPow10Shift[cndigits-1]
else
n += cnLeft.nmNum*10;
if isPrime(n) then
Begin
Cyclops[i] := cnNum;
inc(i);
end;
end;
NextCycNum(result);
end;
repeat
with result do
if isPrime(cnNum) then
Begin
n:= cnRight.nmNum;
if cndigits > 0 then
n += cnLeft.nmNum*cnPow10Shift[cndigits-1]
else
n += cnLeft.nmNum*10;
isPr:= isPrime(n);
inc(i,Ord(isPr));
if isPr AND (cnNum > BIGLIMIT) then
BREAK;
end;
NextCycNum(result);
until false;
result.cnIdx := i;
end;
function FirstPalinPrimeCyclops(cnt:NativeInt):tCN;
var
i: NativeInt;
begin
i := 0;
setlength(Cyclops,cnt);
InitCycNum(result);
while i< cnt do
Begin
MakePalinCycNum(result);
with result do
if isPrime(cnNum) then
Begin
Cyclops[i] := cnNum;
inc(i);
end;
IncLeftCn(result);
while Not(result.cnLeft.nmdgts[result.cnDigits]+1 in [1,3,7,9]) do
IncLeftCn(result);
end;
repeat
MakePalinCycNum(result);
with result do
if isPrime(cnNum) then
begin
inc(i);
if cnNum >BIGLIMIT then
break;
end;
IncLeftCn(result);
while Not(result.cnLeft.nmdgts[result.cnDigits]+1 in [1,3,7,9]) do
IncLeftCn(result);
until false;
result.cnIdx := i;
end;
var
cycnum:tCN;
T0 : Int64;
cnt : NativeUint;
begin
InitCnMinPow;
cnt := 50;
writeln('The first ',cnt,' cyclops numbers are:');
cycnum := FirstCyclops(cnt);
Out_Cyclops(Cyclops,5,10);
writeln('First such number > ',BIGLIMIT,' is ',cycnum.cnNum,
' at zero-based index ',cycnum.cnIdx);
writeln;
cnt := 50;
writeln('The first ',cnt,' prime cyclops numbers are:');
T0 := GetTickCount64;
cycnum := FirstPrimeCyclops(cnt);
T0 := GetTickCOunt64-T0;
Out_Cyclops(Cyclops,7,10);
writeln('First such number > ',BIGLIMIT,' is ',cycnum.cnNum,
' at one-based index ',cycnum.cnIdx);
writeln(cycnum.cnIdx,'.th = ',cycnum.cnNum,' in ',T0/1000:6:3,' s');
writeln;
cnt := 50;
writeln('The first ',cnt,' blind prime cyclops numbers are:');
T0 := GetTickCount64;
cycnum := FirstBlindPrimeCyclops(cnt);
T0 := GetTickCOunt64-T0;
Out_Cyclops(Cyclops,7,10);
writeln('First such number > ',BIGLIMIT,' is ',cycnum.cnNum,
' at one-based index ',cycnum.cnIdx);
writeln(cycnum.cnIdx,'.th = ',cycnum.cnNum,' in ',T0/1000:6:3,' s');
writeln;
cnt := 50;
writeln('The first ',cnt,' palindromatic prime cyclops numbers are:');
cycnum := FirstPalinPrimeCyclops(cnt);
Out_Cyclops(Cyclops,15,5);
writeln('First such number > ',BIGLIMIT,' is ',cycnum.cnNum,
' at one-based index ',cycnum.cnIdx);
writeln;
cnt := 100;
repeat
write(cnt:17,'.th = ');
if cnt <= 10*1000 then
Begin
InitCycNum(cycnum);
repeat
NextCycNum(cycnum);
until cycnum.cnIdx = cnt;
write(cycnum.cnNum);
end;
cycnum:= IndexToCyclops(cnt);
writeln(' calc ',cycnum.cnNum);
cnt *= 10;
until cnt >1000*1000*1000*1000*1000;
end.
- Output:
TIO.RUN The first 50 cyclops numbers are: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First such number > 10000000 is 111101111 at zero-based index 538084 The first 50 prime cyclops numbers are: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First such number > 10000000 is 111101129 at one-based index 39320 39320.th = 111101129 in 0.311 s The first 50 blind prime cyclops numbers are: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First such number > 10000000 is 111101161 at one-based index 11394 11394.th = 111101161 in 0.339 s The first 50 palindromatic prime cyclops numbers are: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First such number > 10000000 is 114808411 at one-based index 67 100.th = 11031 calc 11031 1000.th = 23041 calc 23041 10000.th = 1150651 calc 1150651 100000.th = calc 2630161 1000000.th = calc 118804671 10000000.th = calc 298302381 100000000.th = calc 12382046191 1000000000.th = calc 35296098111 10000000000.th = calc 1287360779121 100000000000.th = calc 4171140671231 1000000000000.th = calc 135781601473441 10000000000000.th = calc 484596705297751 100000000000000.th = calc 14431574037376261 1000000000000000.th = calc 57737327021238771 Real time: 0.794 s CPU share: 99.38 %
Perl
<
use strict;
use warnings;
use feature 'say';
use ntheory 'is_prime';
use List::AllUtils 'firstidx';
sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r }
my @cyclops = 0;
for my $exp (0..3) {
my @oom = grep { ! /0/ } 10**$exp .. 10**($exp+1)-1;
for my $l (@oom) {
for my $r (@oom) {
push @cyclops, $l . '0' . $r;
}
}
}
my @prime_cyclops = grep { is_prime $_ } @cyclops;
my @prime_blind = grep { is_prime $_ =~ s/0//r } @prime_cyclops;
my @prime_palindr = grep { $_ eq reverse $_ } @prime_cyclops;
my $upto = 50;
my $over = 10_000_000;
for (
['', @cyclops],
['prime', @prime_cyclops],
['blind prime', @prime_blind],
['palindromic prime', @prime_palindr]) {
my($text,@values) = @$_;
my $i = firstidx { $_ > $over } @values;
say "First $upto $text cyclops numbers:\n" .
(sprintf "@{['%8d' x $upto]}", @values[0..$upto-1]) =~ s/(.{80})/$1\n/gr;
printf "First $text number > %s: %s at (zero based) index: %s\n\n", map { comma($_) } $over, $values[$i], $i;
}
- Output:
First 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First number > 10,000,000: 111,101,111 at (zero based) index: 538,084 First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First prime number > 10,000,000: 111,101,129 at (zero based) index: 39,319 First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First blind prime number > 10,000,000: 111,101,161 at (zero based) index: 11,393 First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First palindromic prime number > 10,000,000: 114,808,411 at (zero based) index: 66
Phix
You can run this online here (expect a blank screen for about 8s).
with javascript_semantics atom t0 = time() function bump(sequence half) -- add a digit to valid halves -- eg {0} --> {1..9} (no zeroes) -- --> {11..99} ("") -- --> {111..999}, etc sequence res = {} for i=1 to length(half) do integer hi = half[i]*10 for digit=1 to 9 do res &= hi+digit end for end for return res end function procedure cyclops(string s="") sequence res = {}, half = {0} -- valid digits, see bump() integer left = 1, -- half[] before the 0 right = 0, -- half[] after the 0 hlen = 1, -- length(half) cpow = 10, -- cyclops power (of 10) bcpow = 1, -- blind cyclops power cn = 0 -- cyclops number (scratch) bool valid = false, bPrime = match("prime",s), bBlind = match("blind",s), bPalin = match("palindromic",s) while length(res)<50 or cn<=1e7 or not valid do right += 1 if right>hlen then right = 1 left += 1 if left>hlen then half = bump(half) hlen = length(half) cpow *= 10 bcpow *= 10 left = 1 end if end if integer lh = half[left], rh = half[right] cn = lh*cpow+rh -- cyclops number valid = not bPrime or is_prime(cn) if valid and bBlind then valid = is_prime(lh*bcpow+rh) end if if valid and bPalin then valid = sprintf("%d",lh) == reverse(sprintf("%d",rh)) end if if valid then res = append(res,sprintf("%7d",cn)) end if end while printf(1,"First 50 %scyclops numbers:\n%s\n",{s,join_by(res[1..50],1,10)}) printf(1,"First %scyclops number > 10,000,000: %s at (one based) index: %d\n\n", {s,res[$],length(res)}) end procedure cyclops() cyclops("prime ") cyclops("blind prime ") cyclops("palindromic prime ") ?elapsed(time()-t0)
- Output:
First 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First cyclops number > 10,000,000: 111101111 at (one based) index: 538085 First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First prime cyclops number > 10,000,000: 111101129 at (one based) index: 39320 First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First blind prime cyclops number > 10,000,000: 111101161 at (one based) index: 11394 First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First palindromic prime cyclops number > 10,000,000: 114808411 at (one based) index: 67 "7.6s"
billionth cyclops number
Finding the billionth number near-instantly. You can run this online here
with javascript_semantics atom t0 = time() function bump(sequence half) -- (exactly the same as above) -- add a digit to valid halves -- eg {0} --> {1..9} (no zeroes) -- --> {11..99} ("") -- --> {111..999}, etc sequence res = {} for i=1 to length(half) do integer hi = half[i]*10 for digit=1 to 9 do res &= hi+digit end for end for return res end function sequence half = {0} -- valid digits, see bump() integer hlen = 1, cpow = 10 -- cyclops power (of 10) integer n = 1_000_000_000 atom hlen2 = 1 while n>hlen2 do n -= hlen2 half = bump(half) hlen = length(half) hlen2 = power(hlen,2) cpow *= 10 end while integer left = floor(n/hlen)+1, -- half[] before the 0 right = remainder(n,hlen)+1 -- half[] after the 0 string fmt = "The 1,000,000,000th cyclops number is %d (%s)\n", e = elapsed(time()-t0) printf(1,fmt,{half[left]*cpow+half[right],e})
- Output:
The 1,000,000,000th cyclops number is 35296098111 (0s)
Python
from sympy import isprime
def print50(a, width=8):
for i, n in enumerate(a):
print(f'{n: {width},}', end='\n' if (i + 1) % 10 == 0 else '')
def generate_cyclops(maxdig=9):
yield 0
for d in range((maxdig + 1) // 2):
arr = [str(i) for i in range(10**d, 10**(d+1)) if not('0' in str(i))]
for left in arr:
for right in arr:
yield int(left + '0' + right)
def generate_prime_cyclops():
for c in generate_cyclops():
if isprime(c):
yield c
def generate_blind_prime_cyclops():
for c in generate_prime_cyclops():
cstr = str(c)
mid = len(cstr) // 2
if isprime(int(cstr[:mid] + cstr[mid+1:])):
yield c
def generate_palindromic_cyclops(maxdig=9):
for d in range((maxdig + 1) // 2):
arr = [str(i) for i in range(10**d, 10**(d+1)) if not('0' in str(i))]
for s in arr:
yield int(s + '0' + s[::-1])
def generate_palindromic_prime_cyclops():
for c in generate_palindromic_cyclops():
if isprime(c):
yield c
print('The first 50 cyclops numbers are:')
gen = generate_cyclops()
print50([next(gen) for _ in range(50)])
for i, c in enumerate(generate_cyclops()):
if c > 10000000:
print(
f'\nThe next cyclops number after 10,000,000 is {c} at position {i:,}.')
break
print('\nThe first 50 prime cyclops numbers are:')
gen = generate_prime_cyclops()
print50([next(gen) for _ in range(50)])
for i, c in enumerate(generate_prime_cyclops()):
if c > 10000000:
print(
f'\nThe next prime cyclops number after 10,000,000 is {c} at position {i:,}.')
break
print('\nThe first 50 blind prime cyclops numbers are:')
gen = generate_blind_prime_cyclops()
print50([next(gen) for _ in range(50)])
for i, c in enumerate(generate_blind_prime_cyclops()):
if c > 10000000:
print(
f'\nThe next blind prime cyclops number after 10,000,000 is {c} at position {i:,}.')
break
print('\nThe first 50 palindromic prime cyclops numbers are:')
gen = generate_palindromic_prime_cyclops()
print50([next(gen) for _ in range(50)], 11)
for i, c in enumerate(generate_palindromic_prime_cyclops()):
if c > 10000000:
print(
f'\nThe next palindromic prime cyclops number after 10,000,000 is {c} at position {i}.')
break
- Output:
The first 50 cyclops numbers are: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 The next cyclops number after 10,000,000 is 111101111 at position 538,084. The first 50 prime cyclops numbers are: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11,027 11,047 11,057 11,059 11,069 11,071 11,083 11,087 11,093 12,011 12,037 12,041 12,043 12,049 12,071 12,073 12,097 13,033 13,037 13,043 13,049 13,063 13,093 13,099 14,011 14,029 14,033 14,051 14,057 14,071 14,081 14,083 14,087 15,013 15,017 The next prime cyclops number after 10,000,000 is 111101129 at position 39,319. The first 50 blind prime cyclops numbers are: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11,071 11,087 11,093 12,037 12,049 12,097 13,099 14,029 14,033 14,051 14,071 14,081 14,083 14,087 15,031 15,053 15,083 16,057 16,063 16,067 16,069 16,097 17,021 17,033 17,041 17,047 17,053 17,077 18,047 18,061 18,077 18,089 19,013 19,031 19,051 19,073 The next blind prime cyclops number after 10,000,000 is 111101161 at position 11,393. The first 50 palindromic prime cyclops numbers are: 101 16,061 31,013 35,053 38,083 73,037 74,047 91,019 94,049 1,120,211 1,150,511 1,160,611 1,180,811 1,190,911 1,250,521 1,280,821 1,360,631 1,390,931 1,490,941 1,520,251 1,550,551 1,580,851 1,630,361 1,640,461 1,660,661 1,670,761 1,730,371 1,820,281 1,880,881 1,930,391 1,970,791 3,140,413 3,160,613 3,260,623 3,310,133 3,380,833 3,460,643 3,470,743 3,590,953 3,670,763 3,680,863 3,970,793 7,190,917 7,250,527 7,310,137 7,540,457 7,630,367 7,690,967 7,750,577 7,820,287 The next palindromic prime cyclops number after 10,000,000 is 114808411 at position 66.
Raku
<
use Lingua::EN::Numbers;
my @cyclops = 0, |flat lazy ^∞ .map: -> $exp {
my @oom = (exp($exp, 10) ..^ exp($exp + 1, 10)).grep: { !.contains: 0 }
|@oom.hyper.map: { $_ ~ 0 «~« @oom }
}
my @prime-cyclops = @cyclops.grep: { .is-prime };
for '', @cyclops,
'prime ', @prime-cyclops,
'blind prime ', @prime-cyclops.grep( { .trans('0' => '').is-prime } ),
'palindromic prime ', @prime-cyclops.grep( { $_ eq .flip } )
-> $type, $iterator {
say "\n\nFirst 50 {$type}cyclops numbers:\n" ~ $iterator[^50].batch(10)».fmt("%7d").join("\n") ~
"\n\nFirst {$type}cyclops number > 10,000,000: " ~ comma($iterator.first: * > 1e7 ) ~
" - at (zero based) index: " ~ comma $iterator.first: * > 1e7, :k;
}
- Output:
First 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First cyclops number > 10,000,000: 111,101,111 - at (zero based) index: 538,084 First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First prime cyclops number > 10,000,000: 111,101,129 - at (zero based) index: 39,319 First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First blind prime cyclops number > 10,000,000: 111,101,161 - at (zero based) index: 11,393 First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First palindromic prime cyclops number > 10,000,000: 114,808,411 - at (zero based) index: 66
REXX
Version 1
<
/*REXX pgm finds 1st N cyclops (Θ) #s, Θ primes, blind Θ primes, palindromic Θ primes*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
call genP /*build array of semaphores for primes.*/
w= max(10, length( commas(@.#) ) ) /*max width of a number in any column. */
pri?= 0; bli?= 0; pal?= 0; call 0 ' first ' commas(n) " cyclops numbers"
pri?= 1; bli?= 0; pal?= 0; call 0 ' first ' commas(n) " prime cyclops numbers"
pri?= 1; bli?= 1; pal?= 0; call 0 ' first ' commas(n) " blind prime cyclops numbers"
pri?= 1; bli?= 0; pal?= 1; call 0 ' first ' commas(n) " palindromic prime cyclops numbers"
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
0: parse arg title; idx= 1 /*get the title of this output section.*/
say ' index │'center(title, 1 + cols*(w+1) ) /*display the output title. */
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " " separator*/
finds= 0; $= /*the number of finds (so far); $ list.*/
do j=0 until finds== n; L= length(j) /*find N cyclops numbers, start at 101.*/
if L//2==0 then do; j= left(1, L+1, 0) /*Is J an even # of digits? Yes, bump J*/
iterate /*use a new J that has odd # of digits.*/
end
z= pos(0, j); if z\==(L+1)%2 then iterate /* " " " " (zero in mid)? " */
if pos(0, j, z+1)>0 then iterate /* " " " " (has two 0's)? " */
if pri? then if \!.j then iterate /*Need a cyclops prime? Then skip.*/
if bli? then do; ?= space(translate(j, , 0), 0) /*Need a blind cyclops prime ?*/
if \!.? then iterate /*Not a blind cyclops prime? Then skip.*/
end
if pal? then do; r= reverse(j) /*Need a palindromic cyclops prime? */
if r\==j then iterate /*Cyclops number not palindromic? Skip.*/
if \!.r then iterate /* " palindrome not prime? " */
end
finds= finds + 1 /*bump the number of palindromic primes*/
$= $ right( commas(j), w) /*add a palindromic prime ──► $ list.*/
if finds//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible show residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─'); say
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !.= 0; hip= 7890987 - 1 /*placeholders for primes (semaphores).*/
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13 /*define some low primes. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1 /* " " " " flags. */
#= 6; sq.#= @.# ** 2 /*number of primes so far; prime square*/
do j=@.#+2 by 2 for max(0, hip%2-@.#%2-1) /*find odd primes from here on. */
parse var j '' -1 _ /*get the last dec. digit of J.*/
if _==5 then iterate; if j// 3==0 then iterate /*÷ by 5? ÷ by 3? Skip.*/
if j// 7==0 then iterate; if j//11==0 then iterate /*÷ " 7? ÷ by 11? " */
do k=6 while sq.k<=j /* [↓] divide by the known odd primes.*/
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # Ps; assign next P; P sq; P# */
end /*j*/; return
- output when using the default inputs:
index │ first 50 cyclops numbers ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 0 101 102 103 104 105 106 107 108 109 11 │ 201 202 203 204 205 206 207 208 209 301 21 │ 302 303 304 305 306 307 308 309 401 402 31 │ 403 404 405 406 407 408 409 501 502 503 41 │ 504 505 506 507 508 509 601 602 603 604 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── index │ first 50 prime cyclops numbers ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 101 103 107 109 307 401 409 503 509 601 11 │ 607 701 709 809 907 11,027 11,047 11,057 11,059 11,069 21 │ 11,071 11,083 11,087 11,093 12,011 12,037 12,041 12,043 12,049 12,071 31 │ 12,073 12,097 13,033 13,037 13,043 13,049 13,063 13,093 13,099 14,011 41 │ 14,029 14,033 14,051 14,057 14,071 14,081 14,083 14,087 15,013 15,017 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── index │ first 50 blind prime cyclops numbers ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 101 103 107 109 307 401 503 509 601 607 11 │ 701 709 809 907 11,071 11,087 11,093 12,037 12,049 12,097 21 │ 13,099 14,029 14,033 14,051 14,071 14,081 14,083 14,087 15,031 15,053 31 │ 15,083 16,057 16,063 16,067 16,069 16,097 17,021 17,033 17,041 17,047 41 │ 17,053 17,077 18,047 18,061 18,077 18,089 19,013 19,031 19,051 19,073 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── index │ first 50 palindromic prime cyclops numbers ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 101 16,061 31,013 35,053 38,083 73,037 74,047 91,019 94,049 1,120,211 11 │ 1,150,511 1,160,611 1,180,811 1,190,911 1,250,521 1,280,821 1,360,631 1,390,931 1,490,941 1,520,251 21 │ 1,550,551 1,580,851 1,630,361 1,640,461 1,660,661 1,670,761 1,730,371 1,820,281 1,880,881 1,930,391 31 │ 1,970,791 3,140,413 3,160,613 3,260,623 3,310,133 3,380,833 3,460,643 3,470,743 3,590,953 3,670,763 41 │ 3,680,863 3,970,793 7,190,917 7,250,527 7,310,137 7,540,457 7,630,367 7,690,967 7,750,577 7,820,287 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 100.365 seconds. Stretched task not tried.
Version 2: Simplified and using REXX libraries
Libraries: How to use
Library: Numbers
Library: Functions
Library: Settings
Library: Abend
Version 1 generates and flags all needed primes to avoid primality tests. Furthermore, a set of numbers is run thru and tested on 'IsCyclops?' (as many other entries also do). The 4 output procedures were combined in 1 procedure using parameters. All this resulted in a rather complicated program, hard to read and hard to follow.
Now, in REXX everything is 'string'. I can freely intermix numeric and character operations on a variable. No functions like str->num or num->str needed. Therefore procedure GetCyclops is in essence operating on strings only, efficient and very fast generating all cyclop numbers < 115 million. For readability, the 4 output procedures were not combined. The primality check Prime() is in library Numbers.
Without the stretched tasks, version 2 runs in < 1s. With the stretched tasks, most time is spent on the primality tests only for finding the index of the first number > 1e7. Still the results are quite satisfactory.
include Settings
say version; say 'Cyclops numbers'; say
numeric digits 10; cycl. = 0
call GetCyclops 115e6
call Cyclops
call PrimeCyclops
call BlindPrimeCyclops
call PalPrimeCyclops
say Format(Time('e'),,3) 'seconds'
exit
GetCyclops:
procedure expose cycl.
arg x
m = 0; n = 1; cycl.cyclop.1 = 0
do f = 1
h = m+1; m = n
do j = 1 to 9
do i = h to m
a = cycl.cyclop.i
do k = 1 to 9
b = j||a||k
if b > x then
leave f
n = n+1; cycl.cyclop.n = b
end
end
end
end
cycl.0 = n
say n 'cyclops numbers generated <' x; say
return
Cyclops:
procedure expose cycl.
say 'First 50 cyclop numbers:'
do i = 1 to cycl.0
a = cycl.cyclop.i
if i <= 50 then do
call Charout ,Right(a,8)
if i//10 = 0 then
say
end
if a > 1e7 then do
say 'First such number > 1e7 is' a 'at index' i; say
leave i
end
end
return
PrimeCyclops:
procedure expose cycl.
say 'First 50 prime cyclop numbers:'
n = 0
do i = 2 to cycl.0
a = cycl.cyclop.i
if IsPrime(a) then do
n = n+1
if n <= 50 then do
call Charout ,Right(a,8)
if n//10 = 0 then
say
end
if a > 1e7 then do
say 'First such number > 1e7 is' a 'at index' n; say
leave i
end
end
end
return
BlindPrimeCyclops:
procedure expose cycl.
say 'First 50 blind prime cyclop numbers:'
n = 0
do i = 2 to cycl.0
a = cycl.cyclop.i
if IsPrime(a) then do
l = Length(a); m = l%2; b = Left(a,m)||Right(a,m)
if IsPrime(b) then do
n = n+1
if n <= 50 then do
call Charout ,Right(a,8)
if n//10 = 0 then
say
end
if a > 1e7 then do
say 'First such number > 1e7 is' a 'at index' n; say
leave i
end
end
end
end
return
PalPrimeCyclops:
procedure expose cycl.
say 'First 50 palindromic prime cyclop numbers:'
n = 0
do i = 2 to cycl.0
a = cycl.cyclop.i
if a = Reverse(a) then do
if IsPrime(a) then do
n = n+1
if n <= 50 then do
call Charout ,Right(a,8)
if n//10 = 0 then
say
end
if a > 1e7 then do
say 'First such number > 1e7 is' a 'at index' n; say
leave i
end
end
end
end
return
include Numbers
include Functions
- Output:
REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022 Cyclops numbers 774280 cyclops numbers generated < 115E6 First 50 cyclop numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First such number > 1e7 is 111101111 at index 538085 First 50 prime cyclop numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First such number > 1e7 is 111101129 at index 39320 First 50 blind prime cyclop numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First such number > 1e7 is 111101161 at index 11394 First 50 palindromic prime cyclop numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First such number > 1e7 is 114808411 at index 67 20.951 seconds
Ruby
<
require 'prime'
NONZEROS = %w(1 2 3 4 5 6 7 8 9)
cyclopes = Enumerator.new do |y|
(0..).each do |n|
NONZEROS.repeated_permutation(n) do |lside|
NONZEROS.repeated_permutation(n) do |rside|
y << (lside.join + "0" + rside.join).to_i
end
end
end
end
prime_cyclopes = Enumerator.new {|y| cyclopes.each {|c| y << c if c.prime?} }
blind_prime_cyclopes = Enumerator.new {|y| prime_cyclopes.each {|c| y << c if c.to_s.delete("0").to_i.prime?} }
palindromic_prime_cyclopes = Enumerator.new {|y| prime_cyclopes.each {|c| y << c if c.to_s == c.to_s.reverse} }
n, m = 50, 10_000_000
["cyclopes", "prime cyclopes", "blind prime cyclopes", "palindromic prime cyclopes"].zip(
[cyclopes, prime_cyclopes, blind_prime_cyclopes, palindromic_prime_cyclopes]).each do |name, enum|
cycl, idx = enum.each_with_index.detect{|n, i| n > m}
puts "The first #{n} #{name} are: \n#{enum.take(n).to_a}\nFirst #{name} term > #{m}: #{cycl} at index: #{idx}.", ""
end
- Output:
The first 50 cyclopes are: [0, 101, 102, 103, 104, 105, 106, 107, 108, 109, 201, 202, 203, 204, 205, 206, 207, 208, 209, 301, 302, 303, 304, 305, 306, 307, 308, 309, 401, 402, 403, 404, 405, 406, 407, 408, 409, 501, 502, 503, 504, 505, 506, 507, 508, 509, 601, 602, 603, 604] First cyclopes term > 10000000: 111101111 at index: 538084. The first 50 prime cyclopes are: [101, 103, 107, 109, 307, 401, 409, 503, 509, 601, 607, 701, 709, 809, 907, 11027, 11047, 11057, 11059, 11069, 11071, 11083, 11087, 11093, 12011, 12037, 12041, 12043, 12049, 12071, 12073, 12097, 13033, 13037, 13043, 13049, 13063, 13093, 13099, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087, 15013, 15017] First prime cyclopes term > 10000000: 111101129 at index: 39319. The first 50 blind prime cyclopes are: [101, 103, 107, 109, 307, 401, 503, 509, 601, 607, 701, 709, 809, 907, 11071, 11087, 11093, 12037, 12049, 12097, 13099, 14029, 14033, 14051, 14071, 14081, 14083, 14087, 15031, 15053, 15083, 16057, 16063, 16067, 16069, 16097, 17021, 17033, 17041, 17047, 17053, 17077, 18047, 18061, 18077, 18089, 19013, 19031, 19051, 19073] First blind prime cyclopes term > 10000000: 111101161 at index: 11393. The first 50 palindromic prime cyclopes are: [101, 16061, 31013, 35053, 38083, 73037, 74047, 91019, 94049, 1120211, 1150511, 1160611, 1180811, 1190911, 1250521, 1280821, 1360631, 1390931, 1490941, 1520251, 1550551, 1580851, 1630361, 1640461, 1660661, 1670761, 1730371, 1820281, 1880881, 1930391, 1970791, 3140413, 3160613, 3260623, 3310133, 3380833, 3460643, 3470743, 3590953, 3670763, 3680863, 3970793, 7190917, 7250527, 7310137, 7540457, 7630367, 7690967, 7750577, 7820287] First palindromic prime cyclopes term > 10000000: 114808411 at index: 66.
Sidef
<
func cyclops_numbers(base = 10) {
Enumerator({|callback|
var digits = @(1 .. base-1)
for k in (0 .. Inf `by` 2) {
digits.variations_with_repetition(k, {|*a|
a = (a.first(a.len>>1) + [0] + a.last(a.len>>1))
callback(a.flip.digits2num(base))
})
}
})
}
func palindromic_cyclops_numbers(base = 10) {
Enumerator({|callback|
var digits = @(1 .. base-1)
for k in (0..Inf) {
digits.variations_with_repetition(k, {|*a|
a = (a + [0] + a.flip)
callback(a.flip.digits2num(base))
})
}
})
}
func prime_cyclops(base = 10) {
var iter = cyclops_numbers(base)
Enumerator({|callback|
iter.each {|n|
callback(n) if n.is_prime
}
})
}
func blind_prime_cyclops(base = 10) {
var iter = prime_cyclops(base)
Enumerator({|callback|
iter.each {|n|
var k = (n.len(base)-1)>>1
var r = ipow(base, k)
if (r*idiv(n, r*base) + n%r -> is_prime) {
callback(n)
}
}
})
}
func palindromic_prime_cyclops(base = 10) {
var iter = palindromic_cyclops_numbers(base)
Enumerator({|callback|
iter.each {|n|
callback(n) if n.is_prime
}
})
}
for text,f in ([
['', cyclops_numbers],
['prime', prime_cyclops],
['blind prime', blind_prime_cyclops],
['palindromic prime', palindromic_prime_cyclops],
]) {
with (50) {|k|
say "First #{k} #{text} cyclops numbers:"
f().first(k).each_slice(10, {|*a|
a.map { '%7s' % _ }.join(' ').say
})
}
var min = 10_000_000
var iter = f()
var index = 0
var arr = Enumerator({|callback|
iter.each {|n|
callback([index, n]) if (n > min)
++index
}
}).first(1)[0]
say "\nFirst #{text} term > #{min.commify}: #{arr[1].commify} at (zero based) index: #{arr[0].commify}\n"
}
- Output:
First 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First term > 10,000,000: 111,101,111 at (zero based) index: 538,084 First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First prime term > 10,000,000: 111,101,129 at (zero based) index: 39,319 First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First blind prime term > 10,000,000: 111,101,161 at (zero based) index: 11,393 First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First palindromic prime term > 10,000,000: 114,808,411 at (zero based) index: 66
Wren
import "./math" for Int
import "./fmt" for Fmt
import "./str" for Str
var findFirst = Fn.new { |list|
var i = 0
for (n in list) {
if (n > 1e7) return [n, i]
i = i + 1
}
}
var ranges = [0..0, 101..909, 11011..99099, 1110111..9990999, 111101111..119101111]
var cyclops = []
for (r in ranges) {
var numDigits = r.from.toString.count
var center = (numDigits / 2).floor
for (i in r) {
var digits = Int.digits(i)
if (digits[center] == 0 && digits.count { |d| d == 0 } == 1) cyclops.add(i)
}
}
System.print("The first 50 cyclops numbers are:")
var candidates = cyclops[0...50]
var ni = findFirst.call(cyclops)
Fmt.tprint("$,6d", candidates, 10)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])
System.print("\n\nThe first 50 prime cyclops numbers are:")
var primes = cyclops.where { |n| Int.isPrime(n) }
candidates = primes.take(50).toList
ni = findFirst.call(primes)
Fmt.tprint("$,6d", candidates, 10)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])
System.print("\n\nThe first 50 blind prime cyclops numbers are:")
var bpcyclops = []
var ppcyclops = []
for (p in primes) {
var ps = p.toString
var numDigits = ps.count
var center = (numDigits/2).floor
var noMiddle = Num.fromString(Str.delete(ps, center))
if (Int.isPrime(noMiddle)) bpcyclops.add(p)
if (ps == ps[-1..0]) ppcyclops.add(p)
}
candidates = bpcyclops[0...50]
ni = findFirst.call(bpcyclops)
Fmt.tprint("$,6d", candidates, 10)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])
System.print("\n\nThe first 50 palindromic prime cyclops numbers are:")
candidates = ppcyclops[0...50]
ni = findFirst.call(ppcyclops)
Fmt.tprint("$,9d", candidates, 8)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])
- Output:
The first 50 cyclops numbers are: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First such number > 10 million is 111,101,111 at zero-based index 538,084 The first 50 prime cyclops numbers are: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11,027 11,047 11,057 11,059 11,069 11,071 11,083 11,087 11,093 12,011 12,037 12,041 12,043 12,049 12,071 12,073 12,097 13,033 13,037 13,043 13,049 13,063 13,093 13,099 14,011 14,029 14,033 14,051 14,057 14,071 14,081 14,083 14,087 15,013 15,017 First such number > 10 million is 111,101,129 at zero-based index 39,319 The first 50 blind prime cyclops numbers are: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11,071 11,087 11,093 12,037 12,049 12,097 13,099 14,029 14,033 14,051 14,071 14,081 14,083 14,087 15,031 15,053 15,083 16,057 16,063 16,067 16,069 16,097 17,021 17,033 17,041 17,047 17,053 17,077 18,047 18,061 18,077 18,089 19,013 19,031 19,051 19,073 First such number > 10 million is 111,101,161 at zero-based index 11,393 The first 50 palindromic prime cyclops numbers are: 101 16,061 31,013 35,053 38,083 73,037 74,047 91,019 94,049 1,120,211 1,150,511 1,160,611 1,180,811 1,190,911 1,250,521 1,280,821 1,360,631 1,390,931 1,490,941 1,520,251 1,550,551 1,580,851 1,630,361 1,640,461 1,660,661 1,670,761 1,730,371 1,820,281 1,880,881 1,930,391 1,970,791 3,140,413 3,160,613 3,260,623 3,310,133 3,380,833 3,460,643 3,470,743 3,590,953 3,670,763 3,680,863 3,970,793 7,190,917 7,250,527 7,310,137 7,540,457 7,630,367 7,690,967 7,750,577 7,820,287 First such number > 10 million is 114,808,411 at zero-based index 66
XPL0
<
func IsCyclops(N); \Return 'true' if N is a cyclops number
int N, I, J, K;
char A(9);
[I:= 0; \parse digits into array A
repeat N:= N/10;
A(I):= rem(0);
I:= I+1;
until N=0;
if (I&1) = 0 then return false; \must have odd number of digits
K:= I>>1;
if A(K) # 0 then return false; \middle digit must be 0
for J:= 0 to I-1 do \other digits must not be 0
if A(J)=0 & J#K then return false;
return true;
];
func IsPrime(N); \Return 'true' if N > 2 is a prime number
int N, I;
[if (N&1) = 0 \even number\ then return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
func Blind(N); \Return blinded cyclops number
int N, I, J, K; \i.e. center zero removed
char A(9);
[I:= 0; \parse digits into array A
repeat N:= N/10;
A(I):= rem(0);
I:= I+1;
until N=0;
N:= A(I-1); \most significant digit
K:= I>>1;
for J:= I-2 downto 0 do
if J#K then \skip middle zero
N:= N*10 + A(J);
return N;
];
func Reverse(N); \Return N with its digits reversed
int N, M;
[M:= 0;
repeat N:= N/10;
M:= M*10 + rem(0);
until N=0;
return M;
];
func IntLen(N); \Return number of decimal digits in N
int N;
int P, I;
[P:= 10;
for I:= 1 to 9 do \assumes N is 32-bits
[if P>N then return I;
P:= P*10;
];
return 10;
];
int Count, N;
func Show; \Show results and return 'true' when done
[Count:= Count+1;
if Count <= 50 then
[IntOut(0, N);
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
if N > 10_000_000 then
[Text(0, "First such number > 10,000,000: ");
IntOut(0, N);
Text(0, " at zero based index: ");
IntOut(0, Count-1);
CrLf(0);
return true;
];
return false;
];
proc Common(Filter); \Common code gathered here
int Filter;
[Count:= 0;
N:= 0;
loop [if IsCyclops(N) then
case Filter of
0: if Show then quit;
1: if IsPrime(N) then
if Show then quit;
2: if IsPrime(N) then if IsPrime(Blind(N)) then
if Show then quit;
3: if N=Reverse(N) then if IsPrime(N) then
if Show then quit
other [];
N:= N+1;
if (IntLen(N)&1) = 0 then N:= N*10; \must have odd number of digits
];
];
[Text(0, "First 50 cyclops numbers:
");
Common(0);
Text(0, "
First 50 prime cyclops numbers:
");
Common(1);
Text(0, "
First 50 blind prime cyclops numbers:
");
Common(2);
Text(0, "
First 50 palindromic prime cyclops numbers:
");
Common(3);
]
- Output:
First 50 cyclops numbers: 0 101 102 103 104 105 106 107 108 109 201 202 203 204 205 206 207 208 209 301 302 303 304 305 306 307 308 309 401 402 403 404 405 406 407 408 409 501 502 503 504 505 506 507 508 509 601 602 603 604 First such number > 10,000,000: 111101111 at zero based index: 538084 First 50 prime cyclops numbers: 101 103 107 109 307 401 409 503 509 601 607 701 709 809 907 11027 11047 11057 11059 11069 11071 11083 11087 11093 12011 12037 12041 12043 12049 12071 12073 12097 13033 13037 13043 13049 13063 13093 13099 14011 14029 14033 14051 14057 14071 14081 14083 14087 15013 15017 First such number > 10,000,000: 111101129 at zero based index: 39319 First 50 blind prime cyclops numbers: 101 103 107 109 307 401 503 509 601 607 701 709 809 907 11071 11087 11093 12037 12049 12097 13099 14029 14033 14051 14071 14081 14083 14087 15031 15053 15083 16057 16063 16067 16069 16097 17021 17033 17041 17047 17053 17077 18047 18061 18077 18089 19013 19031 19051 19073 First such number > 10,000,000: 111101161 at zero based index: 11393 First 50 palindromic prime cyclops numbers: 101 16061 31013 35053 38083 73037 74047 91019 94049 1120211 1150511 1160611 1180811 1190911 1250521 1280821 1360631 1390931 1490941 1520251 1550551 1580851 1630361 1640461 1660661 1670761 1730371 1820281 1880881 1930391 1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763 3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287 First such number > 10,000,000: 114808411 at zero based index: 66
- Programming Tasks
- Prime Numbers
- 11l
- ALGOL 68
- ALGOL 68-primes
- AppleScript
- Arturo
- AWK
- BBC BASIC
- C++
- C sharp
- Delphi
- SysUtils,StdCtrls,StrUtils,Math
- EasyLang
- Elixir
- F Sharp
- Factor
- FreeBASIC
- FutureBasic
- Go
- Go-rcu
- Haskell
- J
- Java
- Jq
- Julia
- Ksh
- Mathematica
- Wolfram Language
- Nim
- Pascal
- Perl
- Ntheory
- Phix
- Phix/online
- Python
- Raku
- REXX
- Ruby
- Sidef
- Wren
- Wren-math
- Wren-fmt
- Wren-str
- XPL0