Last active
June 20, 2024 10:56
-
-
Save tompng/f8b48738d5e3d2dc413d2bcf1851f71a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prime_test?(n, a) | |
a.pow(n-1,n)==1 | |
end | |
def ruby_mark(n,m,x,y) | |
0.5*x.abs+y.abs<n && y>-m | |
end | |
def pattern(ow, oh, iw, ih) | |
((1-oh)..ow).map{|y| | |
(-(ow*2).round..ow*2-1).map{|x| | |
ruby_mark(iw,ih,x+0.5,y) ? 'a' : ruby_mark(ow,oh,x+0.5,y) ? 'b' : 'c' | |
}.join | |
} | |
end | |
# puts (-5..10).map{|y|(-21..20).map{|x|ruby_mark(10.5,6,x+0.5,y)!=ruby_mark(8.5,4,x+0.5,y) ? '#' : ' '}.join} | |
# puts (-4..8).map{|y|(-17..16).map{|x|ruby_mark(8.5,5,x+0.5,y)!=ruby_mark(6.5,3,x+0.5,y) ? '#' : ' '}.join} | |
# puts (-4..8).map{|y|(-17..16).map{|x|ruby_mark(8.5,5,x+0.5,y)!=ruby_mark(6.5,3,x+0.5,y) ? '#' : ' '}.join} | |
def generate | |
[ | |
[8.5,5,6.5,3], | |
[8.5,5,7,4], | |
[8.5,5,7,3], | |
[8.5,5,6,3], | |
[8.5,5,5,2], | |
[8.5,5,7.5,4], | |
[8.5,5,0,0], | |
[7.5,5,6.5,4], | |
[7.5,5,6,4], | |
[7.5,5,6,3], | |
[7.5,5,5.5,3], | |
[7.5,5,0,0], | |
[7.5,4,6.5,3], | |
[7.5,4,5.5,2], | |
[7.5,4,5,2], | |
[7.5,4,0,0], | |
[6.5,4,5.5,3], | |
[6.5,4,5,2], | |
[6.5,4,4.5,2], | |
[6.5,4,0,0] | |
].reverse_each do |ow,oh,iw,ih| | |
lines = pattern(ow,oh,iw,ih) | |
[[0,0],[1,1],[2,1],[2,2],[3,1],[3,2],[3,3]].each do |mx,my| | |
l = ('c'*(lines[0].size+2*mx)+"\n") | |
yield l*my+lines.map{'c'*mx+_1+'c'*mx+"\n"}.join+l*my | |
end | |
end | |
end | |
generate do |s| | |
bg = 1 | |
(2..9).each do |max| | |
nums = (2..max).to_a | |
nums.size.times do |start| | |
ns = s.gsub(/a|c/, bg.to_s).lines.map.with_index do |l,i| | |
l.chars.map.with_index do |c, j| | |
c.match?(/b/) ? nums[(start+i+j/2)%nums.size] : c | |
end.join | |
end.join | |
n = ns.delete("\n").to_i | |
next if n%2==0||n%3==0||n%5==0||n%7==0 | |
if prime_test?(n, 2) && prime_test?(n, 3) && prime_test?(n, 5) && prime_test?(n, 7) | |
puts ns | |
puts | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prime_test?(n, a) | |
a.pow(n-1,n)==1 | |
end | |
def generate | |
pattern = <<~EOS | |
1111112222222222222222222111111 | |
1111223333333333333333333221111 | |
1122334444444444444444444332211 | |
2233444444444444444444444443322 | |
1122334444444444444444444332211 | |
1111223344444444444444433221111 | |
1111112233444444444443322111111 | |
1111111122334444444332211111111 | |
1111111111223344433221111111111 | |
1111111111112233322111111111111 | |
1111111111111122211111111111111 | |
EOS | |
lines = pattern.chomp.split("\n") | |
[[0,0],[1,0],[2,0],[0,1],[0,2],[1,2],[1,1],[2,1],[2,2],[3,1],[3,2],[3,3]].each do |mx,my| | |
l = ('1'*(lines[0].size+2*mx)+"\n") | |
yield l*my+lines.map{'1'*mx+_1+'1'*mx+"\n"}.join+l*my | |
end | |
end | |
generate do |s| | |
[*2..9].product([*1..9],[*1..9]).each do |a,b,c| | |
ns = s.tr('234', [a,b,c].join) | |
n = ns.delete("\n").to_i | |
next if n%2==0||n%3==0||n%5==0||n%7==0 | |
if prime_test?(n, 2) && prime_test?(n, 3) && prime_test?(n, 5) && prime_test?(n, 7) | |
puts ns | |
puts | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faster_prime' | |
n = %w( | |
1111111111111111111111111111111 | |
1111118888888888888888888111111 | |
1111888888888888888888888881111 | |
1188881111111111111111111888811 | |
8888111111111111111111111118888 | |
1188881111111111111111111888811 | |
1111888811111111111111188881111 | |
1111118888111111111118888111111 | |
1111111188881111111888811111111 | |
1111111111888811188881111111111 | |
1111111111118888888111111111111 | |
1111111111111188811111111111111 | |
1111111111111111111111111111111 | |
).join.to_i | |
puts "#{n}.prime? #=> #{n.prime?}" | |
n = %w( | |
111111111111111111111111111111111111 | |
111111111677889922334455667111111111 | |
111111167788992233445566778891111111 | |
111116778899223344556677889922311111 | |
111677889922334455667788992233445111 | |
111118992233445566778899223344511111 | |
111111123344556677889922334451111111 | |
111111111455667788992233445111111111 | |
111111111116778899223344511111111111 | |
111111111111189922334451111111111111 | |
111111111111111233445111111111111111 | |
111111111111111114511111111111111111 | |
111111111111111111111111111111111111 | |
).join.to_i | |
puts "#{n}.prime? #=> #{n.prime?}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment