diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-01-18 10:37:54 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-01-18 10:37:54 +0000 |
| commit | 5bd157274f9d0377833a954cf48d6b4866615aae (patch) | |
| tree | 804cbbdb2fd3d9de5864c736e5088677f94bf898 /challenge-147/abigail/lua/ch-1.lua | |
| parent | 990d5177887cda20c8a4804b91afe5e9bcaaf201 (diff) | |
| parent | 4c0094aa3f456810b1795784caa1fe06fbdacb5c (diff) | |
| download | perlweeklychallenge-club-5bd157274f9d0377833a954cf48d6b4866615aae.tar.gz perlweeklychallenge-club-5bd157274f9d0377833a954cf48d6b4866615aae.tar.bz2 perlweeklychallenge-club-5bd157274f9d0377833a954cf48d6b4866615aae.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-147/abigail/lua/ch-1.lua')
| -rw-r--r-- | challenge-147/abigail/lua/ch-1.lua | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-147/abigail/lua/ch-1.lua b/challenge-147/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..0c86152ba1 --- /dev/null +++ b/challenge-147/abigail/lua/ch-1.lua @@ -0,0 +1,57 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 +-- + +-- +-- Run as: lua ch-1.lua +-- + +function is_prime (p) + if p == 2 then + return true + end + if p % 2 == 0 then + return false + end + i = 3 + while i * i <= p do + if p % i == 0 then + return false + end + i = i + 2 + end + return true +end + +todo = {2, 3, 5, 7} +for i, p in ipairs (todo) do + io . write (p, " ") +end + +count = 20 - #todo + +pow = 10 +while #todo > 0 do + new_todo = {} + for d = 1, 9 do + for i, p in ipairs (todo) do + candidate = d * pow + p + if is_prime (candidate) then + io . write (candidate, " ") + new_todo [#new_todo + 1] = candidate + count = count - 1 + if count <= 0 then + goto end_of_while + end + end + end + end + todo = new_todo + pow = pow * 10 +end + +::end_of_while:: + +io . write ("\n") |
