Cyclops numbers: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(14 intermediate revisions by 5 users not shown)
Line 624:
1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763
3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> 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</syntaxhighlight>
{{out}}
<pre>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.
</pre>
 
Line 1,026 ⟶ 1,132:
1970791 3140413 3160613 3260623 3310133 3380833 3460643 3470743 3590953 3670763
3680863 3970793 7190917 7250527 7310137 7540457 7630367 7690967 7750577 7820287
 
</pre>
 
=={{header|EasyLang}}==
{{trans|C++}}
<syntaxhighlight>
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
</syntaxhighlight>
 
=={{header|Elixir}}==
<syntaxhighlight lang="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]
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 1,259 ⟶ 1,538:
35,296,098,111
</pre>
 
=={{header|FreeBASIC}}==
{{trans|FutureBasic}}
<syntaxhighlight lang="vbnet">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</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|FutureBasic}}==
Line 1,264 ⟶ 1,705:
begin globals
CFTimeInterval t
long n(16), clops(52), primes(52), blinds(52), pals(52)
long num, iClop, iPrime, iBlind, iPal
byteshort first1, last1, midPt
xref show(50) as long
midPt = 8
Line 1,272 ⟶ 1,713:
 
local fn convertToNumber as long
long p10 = 1, nr = 0
byteshort c2 = last1
do
nr += n(c2) * p10
Line 1,281 ⟶ 1,722:
end fn = nr
 
void local fn increment( dgt as byteshort )
if n(dgt) < 9 then n(dgt)++ : exit fn
n(dgt) = 1
Line 1,305 ⟶ 1,746:
 
local fn isBlind as bool
byteshort temp = 0
swap temp, midPt //Keep fn convertToNumber from adding 0 at midPt
bool rslt = fn isPrime( fn convertToNumber )
Line 1,312 ⟶ 1,753:
 
local fn isPalindrome as bool
byteshort a = first1, b = last1, c
while b > a
if n(a) <> n(b) then exit fn = no
Line 1,320 ⟶ 1,761:
 
void local fn print50( title as cfstringref, addr as ptr )
byteshort r = 0
show = addr //Set array address to param
print : print title
Line 3,521 ⟶ 3,962:
 
=={{header|Wren}}==
{{libheader|Wren-seqmath}}
{{libheader|Wren-fmt}}
{{libheader|Wren-str}}
<<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seqfmt" for LstFmt
import "./fmtstr" for FmtStr
import "/str" for Str
 
var findFirst = Fn.new { |list|
Line 3,551 ⟶ 3,991:
var candidates = cyclops[0...50]
var ni = findFirst.call(cyclops)
Fmt.tprint("$,6d", candidates, 10)
for (chunk in Lst.chunks(candidates, 10)) Fmt.print("$,6d", chunk)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])
 
Line 3,558 ⟶ 3,998:
candidates = primes.take(50).toList
ni = findFirst.call(primes)
Fmt.tprint("$,6d", candidates, 10)
for (chunk in Lst.chunks(candidates, 10)) Fmt.print("$,6d", chunk)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])
 
Line 3,574 ⟶ 4,014:
candidates = bpcyclops[0...50]
ni = findFirst.call(bpcyclops)
Fmt.tprint("$,6d", candidates, 10)
for (chunk in Lst.chunks(candidates, 10)) Fmt.print("$,6d", chunk)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])
 
Line 3,580 ⟶ 4,020:
candidates = ppcyclops[0...50]
ni = findFirst.call(ppcyclops)
Fmt.tprint("$,9d", candidates, 8)
for (chunk in Lst.chunks(candidates, 8)) Fmt.print("$,9d", chunk)
Fmt.print("\nFirst such number > 10 million is $,d at zero-based index $,d", ni[0], ni[1])</syntaxhighlight>
 
2,130

edits