1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# The fibonacci.pl.raku.c Polyglot
> Published at 2014-03-24T21:32:53+00:00; Updated at 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:
```perl
#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++
```sh
% 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
```sh
% 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 `paul@nospam.buetow.org` :-)
=> ../ Back to the main site
|