Generating the Fibonacci sequence on a Raspberry Pi 5
Linux includes a bench calculator called bc, which can achieve remarkable arbitrary-precision results in very few lines of code. For ease of running, code can be embedded in a here document:
fib.sh
#!/usr/bin/env bash
BC_LINE_LENGTH=0 bc -q <<end
0;1; for (i=1;i<1000;i++) {g=last;last+=f;f=g;last}
endThe BC_LINE_LENGTH environment variable is used to prevent long lines from being split with a \ continuation character.
The -q flag suppresses the welcome prompt.
Epic Number Battles of History: Swift 3 vs Python
Swift 3 has a “decimal” number type that can represent values such as 0.3 that are inexactly represented in Double or Float types. According to this article:
An instance can represent any number that can be expressed as
mantissa x 10^exponentwhere mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127.
Note this is not the same as Python’s arbitrary-precision integer arithmetic. This shows up clearly when using Python (on the left) and Swift (on the right) to generate factorial numbers. Differences start to show up at 40! and continue from there.