diff options
| author | Paul Buetow <paul@buetow.org> | 2023-03-12 17:24:03 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-03-12 17:24:03 +0200 |
| commit | 13dfc3619a90d44df3f6e094e0066db5b1898ca6 (patch) | |
| tree | 63565d0806202a73d00d59dfeded47fcf7974ca7 /gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi | |
| parent | 597cc24f91aebd022e8afeef509ef3c5f07e6895 (diff) | |
revert
Diffstat (limited to 'gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi')
| -rw-r--r-- | gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi | 144 |
1 files changed, 143 insertions, 1 deletions
diff --git a/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi b/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi index ad6e91b6..e1b88be2 100644 --- a/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi +++ b/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi @@ -1 +1,143 @@ -Published at 2014-03-24T21:32:53+00:00; Updated at 2022-04-23 +# The fibonacci.pl.raku.c Polyglot + +> Author: Paul; Published: 2014-03-24T21:32:53+00:00; Updated: 2022-04-23 + +In computing, a polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independent of the programming language used to compile or interpret it. + +=> https://en.wikipedia.org/wiki/Polyglot_(computing) + +## The Fibonacci numbers + +For fun, I programmed my own Polyglot, which is both valid Perl, Raku, C and C++ code (I have added C++ and Raku support in 2022). The exciting part about C and C++ is that $ is a valid character to start variable names with: + +``` +#include <stdio.h> + +#define $arg function_argument +#define my int +#define sub int +#define BEGIN int main(void) + +my $arg; + +sub hello() { + printf("Hello, welcome to the Fibonacci Numbers!\n"); + printf("This program is all, valid C and C++ and Perl and Raku code!\n"); + printf("It calculates all fibonacci numbers from 0 to 9!\n\n"); + return 0; +} + +sub fibonacci() { + my $n = $arg; + + if ($n < 2) { + return $n; + } + + $arg = $n - 1; + my $fib1 = fibonacci(); + $arg = $n - 2; + my $fib2 = fibonacci(); + + return $fib1 + $fib2; +} + +BEGIN { + hello(); + my $i = 0; + + while ($i <= 10) { + $arg = $i; + printf("fib(%d) = %d\n", $i, fibonacci()); + $i++; + } +} +``` + +You can find the full source code at GitHub: + +=> https://codeberg.org/snonux/perl-c-fibonacci + +### Let's run it with C and C++ + +``` +% gcc fibonacci.pl.raku.c -o fibonacci +% ./fibonacci +Hello, welcome to the Fibonacci Numbers! +This program is all, valid C and C++ and Perl and Raku code! +It calculates all fibonacci numbers from 0 to 9! + +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 + +% g++ fibonacci.pl.raku.c -o fibonacci +% ./fibonacci +Hello, welcome to the Fibonacci Numbers! +This program is all, valid C and C++ and Perl and Raku code! +It calculates all fibonacci numbers from 0 to 9! + +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 +``` + +### Let's run it with Perl and Raku + +``` +% perl fibonacci.pl.raku.c +Hello, welcome to the Fibonacci Numbers! +This program is all, valid C and C++ and Perl and Raku code! +It calculates all fibonacci numbers from 0 to 9! + +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 + +% raku fibonacci.pl.raku.c +Hello, welcome to the Fibonacci Numbers! +This program is all, valid C and C++ and Perl and Raku code! +It calculates all fibonacci numbers from 0 to 9! + +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 +``` + +It's entertaining to play with :-). + +E-Mail your comments to hi@paul.cyou :-) + +=> ../ Go back to the main site |
