Sexy primes: Difference between revisions
Content added Content deleted
m (Minor edit to C++ code) |
|||
Line 1,046: | Line 1,046: | ||
[999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003] |
[999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003] |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
|||
<lang lua>local N = 1000035 |
|||
-- FUNCS: |
|||
local function T(t) return setmetatable(t, {__index=table}) end |
|||
table.filter = function(t,f) local s=T{} for _,v in ipairs(t) do if f(v) then s[#s+1]=v end end return s end |
|||
table.map = function(t,f,...) local s=T{} for _,v in ipairs(t) do s[#s+1]=f(v,...) end return s end |
|||
table.lastn = function(t,n) local s=T{} n=n>#t and #t or n for i = 1,n do s[i]=t[#t-n+i] end return s end |
|||
table.each = function(t,f,...) for _,v in ipairs(t) do f(v,...) end end |
|||
table.tohash = function(t) local h={} for i=1,#t do h[t[i]]=t[i] end return h end |
|||
-- PRIMES: |
|||
local sieve, primes = {false}, T{} |
|||
for i = 2,N+6 do sieve[i]=true end |
|||
for i = 2,N+6 do if sieve[i] then for j=i*i,N+6,i do sieve[j]=nil end end end |
|||
for i = 2,N+6 do if sieve[i] then primes[#primes+1]=i end end |
|||
-- TASKS: |
|||
local sexy, name = { primes }, { "primes", "pairs", "triplets", "quadruplets", "quintuplets" } |
|||
local function sexy2str(v,n) local s=T{} for i=1,n do s[i]=v+(i-1)*6 end return "("..s:concat(" ")..")" end |
|||
for i = 2, 5 do |
|||
sexy[i] = sexy[i-1]:filter(function(v) return v+(i-1)*6<N and sieve[v+(i-1)*6] end) |
|||
print(#sexy[i] .. " " .. name[i] .. ", ending with: " .. sexy[i]:lastn(5):map(sexy2str,i):concat(" ")) |
|||
end |
|||
local unsexy = primes:filter(function(v) return not (v>=N or sieve[v-6] or sieve[v+6]) end) |
|||
print(#unsexy .. " unsexy, ending with: " ..unsexy:lastn(10):concat(" "))</lang> |
|||
{{out}} |
|||
<pre>16386 pairs, ending with: (999371 999377) (999431 999437) (999721 999727) (999763 999769) (999953 999959) |
|||
2900 triplets, ending with: (997427 997433 997439) (997541 997547 997553) (998071 998077 998083) (998617 998623 998629) (998737 998743 998749) |
|||
325 quadruplets, ending with: (977351 977357 977363 977369) (983771 983777 983783 983789) (986131 986137 986143 986149) (990371 990377 990383 990389) (997091 997097 997103 997109) |
|||
1 quintuplets, ending with: (5 11 17 23 29) |
|||
48627 unsexy, ending with: 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |