diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-11-07 19:02:02 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-11-07 19:02:02 +0000 |
| commit | 8fcb93bc381fc3471dd61d8d66449086c5ac04ad (patch) | |
| tree | 4ce3939a2ddb767a8067f0b3eedb35b3c6a95d40 | |
| parent | 4565c4cf8ace685b2de7f2f2116314181190b649 (diff) | |
| download | perlweeklychallenge-club-8fcb93bc381fc3471dd61d8d66449086c5ac04ad.tar.gz perlweeklychallenge-club-8fcb93bc381fc3471dd61d8d66449086c5ac04ad.tar.bz2 perlweeklychallenge-club-8fcb93bc381fc3471dd61d8d66449086c5ac04ad.zip | |
- Added guest contributions by Laurent Rosenfeld.
| -rw-r--r-- | challenge-137/laurent-rosenfeld/awk/ch-2.awk | 23 | ||||
| -rw-r--r-- | challenge-137/laurent-rosenfeld/bc/ch-2.bc | 31 | ||||
| -rw-r--r-- | challenge-137/laurent-rosenfeld/c/ch-2.c | 42 | ||||
| -rw-r--r-- | challenge-137/laurent-rosenfeld/julia/ch-1.jl | 16 | ||||
| -rw-r--r-- | challenge-137/laurent-rosenfeld/lua/ch-2.lua | 16 | ||||
| -rw-r--r-- | challenge-137/laurent-rosenfeld/python/ch-2.py | 18 | ||||
| -rw-r--r-- | challenge-137/laurent-rosenfeld/ruby/ch-2.rb | 20 | ||||
| -rw-r--r-- | challenge-137/laurent-rosenfeld/rust/ch-2.rs | 16 |
8 files changed, 182 insertions, 0 deletions
diff --git a/challenge-137/laurent-rosenfeld/awk/ch-2.awk b/challenge-137/laurent-rosenfeld/awk/ch-2.awk new file mode 100644 index 0000000000..7dac9b2864 --- /dev/null +++ b/challenge-137/laurent-rosenfeld/awk/ch-2.awk @@ -0,0 +1,23 @@ +#!/usr/bin/awk + +function reverse (num) { + rev = "" + len = length(num) + for (i = len; i > 0; i--) { + rev = rev substr(num, i, 1); + } + return rev +} +function is_lychrel(n) { + for (i = 1; i <= 5; i++) { + if (n > 10000000) { + return "is a Lychrel candidate. Reached the 1e7 limit" + } + rev = reverse(n) + # print n, rev + if (n == rev) { return 0 } + n += rev + } + return "is a lychrel candidate (made 500 iterations)" +} +/[0-9]+/ { print $0, " -> ", is_lychrel($0) } diff --git a/challenge-137/laurent-rosenfeld/bc/ch-2.bc b/challenge-137/laurent-rosenfeld/bc/ch-2.bc new file mode 100644 index 0000000000..fee4dd6f55 --- /dev/null +++ b/challenge-137/laurent-rosenfeld/bc/ch-2.bc @@ -0,0 +1,31 @@ +define reverse (n) { + rev = 0 + while (n > 0) { + rev *= 10 + rev += n % 10 + n /= 10 + } + return (rev) +} + + +define is_lychrel(n) { + for (i = 1; i < 500; i++) { + if (n >= 10000000) { return -1} + rev = reverse(n) + /* print n, " ", rev, "\n" */ + if (n == rev) {return 0;} + n += rev + } + return -1 +} + +while (1) { + n = read () + if (is_lychrel (n) == -1) { + print n, " Lychrel candidate", "\n" + } else { + print n, " ", 0, "\n" + } +} +quit diff --git a/challenge-137/laurent-rosenfeld/c/ch-2.c b/challenge-137/laurent-rosenfeld/c/ch-2.c new file mode 100644 index 0000000000..3ab090b712 --- /dev/null +++ b/challenge-137/laurent-rosenfeld/c/ch-2.c @@ -0,0 +1,42 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#define MAX_ITER 500 +#define MAX_VAL 10000000 +#define NB_TESTS 6 + +void reverse_str(char* str) { + int len, tmp; + len = strlen(str); + for (int i = 0; i < len/2; i++) { + tmp = str[i]; + str[i] = str[len - i - 1]; + str[len - i - 1] = tmp; + } +} + +const char* lychrel (int n) { + char out[20]; + for (int k = 1; k <= MAX_ITER; k++) { + if (n > MAX_VAL) { + return "is a Lychrel candidate. Reached the 1e7 limit"; + } + char to_str[20]; + char rev[20]; + sprintf(to_str, "%d", n); + strcpy(rev, to_str); + reverse_str(rev); + if (strcmp(to_str, rev) == 0) { + return "0"; + } + n += atoi(rev); + } + return "is a Lychrel candidate. Reached 500 iterations"; +} + +int main() { + int tests[NB_TESTS] = { 10, 20, 30, 50, 100, 196}; + for (int i = 0; i < NB_TESTS; i++) { + printf("%d -> %s\n", tests[i], lychrel(tests[i])); + } +} diff --git a/challenge-137/laurent-rosenfeld/julia/ch-1.jl b/challenge-137/laurent-rosenfeld/julia/ch-1.jl new file mode 100644 index 0000000000..4ea2aff39a --- /dev/null +++ b/challenge-137/laurent-rosenfeld/julia/ch-1.jl @@ -0,0 +1,16 @@ +function is_lychrel(n) + m = n + for k = 1:500 + if (n > 10_000_000) + return "$m is a Lychrel candidate. Reached the 1e7 limit" + end + rev = parse(Int64, reverse(string(n))) + if (n == rev) return 0 end + n += rev + end + return "$m is a lychrel candidate (made 500 iterations)"; +end + +for test in [10, 20, 30, 50, 100, 196] + println("$test -> $(is_lychrel(test))") +end diff --git a/challenge-137/laurent-rosenfeld/lua/ch-2.lua b/challenge-137/laurent-rosenfeld/lua/ch-2.lua new file mode 100644 index 0000000000..60ab8f2e76 --- /dev/null +++ b/challenge-137/laurent-rosenfeld/lua/ch-2.lua @@ -0,0 +1,16 @@ +function is_lychrel(n) + m = n + for k =1, 500 do + if n > 10000000 then + return string.format("%s is a Lychrel candidate. Reached the 1e7 limit", m) + end + rev = tonumber(string.reverse(tostring(n))) + if n == rev then return 0 end + n = n + rev + end + return string.format("%s is a lychrel candidate (made 500 iterations)", m); +end + +for key, test in ipairs({10, 20, 30, 50, 100, 196}) do + print(test, " -> ", is_lychrel(test)) +end diff --git a/challenge-137/laurent-rosenfeld/python/ch-2.py b/challenge-137/laurent-rosenfeld/python/ch-2.py new file mode 100644 index 0000000000..2442d978c8 --- /dev/null +++ b/challenge-137/laurent-rosenfeld/python/ch-2.py @@ -0,0 +1,18 @@ +#!/usr/bin/python3 + +def is_lychrel(m): + n = m + for i in range(500): + j = int(str(n)[::-1]) + if j == n: + return 0 + n += j + if n > 10000000: + return "n is a lychrel candidate. Reached the 1e7 limit." + return "n is a lychrel candidate. Made 500 iterations." + +for test in range(10, 20): + print(test, " -> ", is_lychrel(test)) + +for test in 10, 20, 30, 50, 100, 196: + print(test, " -> ", is_lychrel(test)) diff --git a/challenge-137/laurent-rosenfeld/ruby/ch-2.rb b/challenge-137/laurent-rosenfeld/ruby/ch-2.rb new file mode 100644 index 0000000000..1baf7c046c --- /dev/null +++ b/challenge-137/laurent-rosenfeld/ruby/ch-2.rb @@ -0,0 +1,20 @@ +#! /usr/bin/ruby + +def is_lychrel(m) + n = m + for k in 1..500 + j = n.to_s.reverse.to_i + if j == n then + return 0 + end + n += j + if n > 10000000 then + return "#{m} is a Lychrel candidate (reached the 1e7 limit)" + end + end + return "#{m} is a lychrel candidate (made 500 iterations)" +end + +for test in [10, 20, 30, 50, 100, 196] + print "#{test} -> ", is_lychrel(test), "\n" +end diff --git a/challenge-137/laurent-rosenfeld/rust/ch-2.rs b/challenge-137/laurent-rosenfeld/rust/ch-2.rs new file mode 100644 index 0000000000..60ab8f2e76 --- /dev/null +++ b/challenge-137/laurent-rosenfeld/rust/ch-2.rs @@ -0,0 +1,16 @@ +function is_lychrel(n) + m = n + for k =1, 500 do + if n > 10000000 then + return string.format("%s is a Lychrel candidate. Reached the 1e7 limit", m) + end + rev = tonumber(string.reverse(tostring(n))) + if n == rev then return 0 end + n = n + rev + end + return string.format("%s is a lychrel candidate (made 500 iterations)", m); +end + +for key, test in ipairs({10, 20, 30, 50, 100, 196}) do + print(test, " -> ", is_lychrel(test)) +end |
