-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyclops_numbers.sf
115 lines (97 loc) · 3.74 KB
/
cyclops_numbers.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 28 July 2021
# https://github.com/trizen
# Generate cyclops numbers.
# 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.
# See also:
# https://rosettacode.org/wiki/Cyclops_numbers
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 "\nFirst #{k} #{text} cyclops numbers:"
f().first(k).each_slice(10, {|*a|
a.map { '%7s' % _ }.join(' ').say
})
}
}
__END__
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