Perl, Adam KatzPerl, Adam Katz
Description
$_=cos 99
saves 0.0398208803931389 in the variable $_
. If we multiply this amount by 0x7275, we obtain 1166.79161639936, which is roughly 4 times the original output and 8 times the modified output.1
The original code archives almost-division-by-4 by substituting the first 3 in $_
with a 0 by executing s/[3-8]/0/
, which modifies $_
and returns the number of substitutions (1), by which $_
is then divided.2
After noting that there are eight digits between 3 and 8 in the unmodified value of $_
, attempting to replace all of them by appending g
to s/[3-8]/0/
seemed natural, since this will divide the (modified) value of $_
by the number of substitutions.
Finally, to diminish the effect of the almost-division-by-4, it suffices to replace the first 3 (and all occurrences of 3 and 8 that follow) with a 3 by replacing s/[3-8]/0/
with s/[3-8]/3/
.
Code
$_=cos 99;printf"%.14f",0x7275*sin $_/s/[3-8]/3/g
1 The code actually applies sine to $_
before multiplying, but sin x ≈ x when x is close to 0.
2 Again, this completely ignores the sine, which is almost linear near 0.