Shape-Machine
Task
Get user input and then add 3, multiply that 0.86 and repeat forever. You can halt the loop and count how many times does the operation happen, this is optional
See Also
ALGOL 68
Non-terminating
BEGIN
REAL a := BEGIN INT n; read( n ); n END;
DO
print( ( fixed( a +:= 3 *:= 0.86, -16, 14 ), newline ) )
OD
END
Terminating after successive values converge
Based on Raku, using 64-bit floating point.
BEGIN
REAL a := BEGIN INT n; read( n ); n END;
REAL prev a := IF a = 0 THEN 1 ELSE 0 FI;
INT count := 0;
WHILE prev a /= a
DO
count +:= 1;
prev a := a;
print( ( fixed( a +:= 3 *:= 0.86, -16, 14 ), newline ) )
OD;
print( ( whole( count, 0 ), " repetitions" ) )
END
- Output:
With 4 as the input
6.02000000000000 7.75720000000000 9.25119200000000 10.5360251200000 11.6409816032000 12.5912441787520 13.4084699937267 14.1112841946050 ... 18.3988095846536 18.4029762428021 18.4065595688098 ... 18.4285714285714 18.4285714285714 18.4285714285714 231 repetitions
ALGOL W
begin
real a;
integer n;
read( n );
a := n;
while true do begin
a := a + 3;
a := a * 0.86;
write( r_format := "A", r_w := 8, r_d := 5, a )
end
end.
- Output:
With 4 as the input
6.02000 7.75720 9.25119 10.53603 11.64098 12.59124 13.40847 14.11128 ... 18.42857 18.42857 18.42857 ...
jq
Works with jq, the C implementation of jq
Works with gojq, the Go implementation of jq
Works with jaq, the Rust implementation of jq
In accordance with the basic task description, here is a solution that does not terminate:
input | recurse(. + 3 | . * 0.86)
To examine the first five generated items, we could use the shell's `head` utility, e.g. as follows:
$ jq -n ' input | recurse(. + 3 | . * 0.86)' <<< 0 | head -n 5 0 2.58 4.7988 6.706968 8.34799248
Notice that the output of `recurse` includes the input value.
To limit the generator programmatically, one could use the `limit/2` built-in:
$ jq -n --argjson n 5 'input | limit($n; recurse(. + 3 | . * 0.86))' <<< 1 1 3.44 5.538399999999999 7.343023999999999 8.89500064
One way to suppress the first item produced by the generator would be to use `whilst/1`:
# like while/2 but emit the final term rather than the first one
def whilst(cond; update):
def _whilst:
if cond then update | (., _whilst) else empty end;
_whilst;
input | whilst(true; . + 3 | . * 0.86)
To stop when the generated number satisfies `trunc == 18`, one could use the following filter:
whilst(trunc != 18; . + 3 | . * 0.86)
To determine how many iterations it takes until the truncated value is 18, one could use `until/2`:
$ jq -n '{i:input} | until(.i|trunc == 18; .i += 3 | .i *= 0.86 | .n+=1)' <<< 0 { "i": 18.003997862641942, "n": 25 }
Phix
with javascript_semantics
--atom n = prompt_number("") -- (not js compatible)
atom n = 4
integer count = 0
do
printf(1,"%.20f\n",n)
atom prev = n
n = (n + 3) * 0.86
count += 1
until n==prev
printf(1,"Took %d iterations.\n",count)
- Output:
64-bit
4.00000000000000000000 6.01999999999999999998 7.75720000000000000067 9.25119200000000000035 ... 18.42857142857142856499 18.42857142857142856672 18.42857142857142856845 Took 281 iterations.
32-bit
4.00000000000000000000 6.0199999999999996 7.7571999999999992 9.2511919999999996 10.5360251199999997 ... 18.4285714285714093 18.4285714285714128 18.4285714285714164 Took 231 iterations.
Python
a=int(input()) while True: a += 3 a *= 0.86 print(a)
Raku
my $input = (+prompt()).FatRat;
my $previous = 0.FatRat;
my $count;
my $places = 36;
loop {
$input += 3;
$input ×= .86;
last if $previous.substr(0,$places) eq $input.substr(0,$places);
++$count;
say ($previous = $input).substr(0,$places);
}
say "$count repetitions";
- Output with a 4 fed in at the prompt:
4 6.02 7.7572 9.251192 10.53602512 11.6409816032 12.591244178752 13.40846999372672 14.1112841946049792 14.715704407360282112 15.23550579032984261632 15.6825349796836646500352 16.066980082527951599030272 ... many, many lines omitted ... 18.428571428571428571428571428571416 18.428571428571428571428571428571418 18.428571428571428571428571428571419 18.428571428571428571428571428571420 18.428571428571428571428571428571421 18.428571428571428571428571428571422 18.428571428571428571428571428571423 18.428571428571428571428571428571424 512 repetitions
Wren
This repeats until the next number (a 64 bit float) equals the previous number which will usually be sometime later than their string/display values (normally limited to 14 digits in Wren CLI) are equal.
import "./ioutil" for Input
var prev = Input.number("")
var count = 1
while (true) {
var next = (prev + 3) * 0.86
if (prev == next) break
System.print(prev = next)
count = count + 1
}
System.print("Took %(count) iterations.")
- Output:
Sample input/output (abridged):
4 6.02 7.7572 9.251192 10.53602512 11.6409816032 12.591244178752 13.408469993727 14.111284194605 14.71570440736 ... 18.428571428571 18.428571428571 18.428571428571 18.428571428571 18.428571428571 18.428571428571 18.428571428571 18.428571428571 18.428571428571 18.428571428571 Took 231 iterations.
XPL0
real A;
[A:= RlIn(0);
while true do
[A:= A+3.;
A:= A*0.86;
RlOut(0, A);
];
]
- Output:
123 108.36000 95.76960 84.94186 75.63000 67.62180 60.73475 54.81188 49.71822 45.33767 41.57039 38.33054 35.54426 33.14807 31.08734 29.31511 27.79099 26.48026 25.35302 24.38360 23.54989 22.83291 22.21630 21.68602 21.22998 20.83778 20.50049 20.21042 19.96096 19.74643 19.56193 19.40326 19.26680 19.14945 19.04853 18.96173 18.88709 18.82290 18.76769 18.72022 18.67939 18.64427 18.61407 18.58810 18.56577 18.54656 18.53004 18.51584 18.50362 18.49311 18.48408 18.47631 18.46962 18.46388 18.45893 18.45468 18.45103 18.44788 18.44518 18.44285 18.44085 18.43914 18.43766 18.43638 18.43529 18.43435 18.43354 18.43285 18.43225 18.43173 18.43129 18.43091 18.43058 18.43030 18.43006 18.42985 18.42967 18.42952 18.42938 18.42927 18.42917 18.42909 18.42902 18.42895 18.42890 18.42885 18.42881 18.42878 18.42875 18.42873 18.42870 18.42869 18.42867 18.42866 18.42864 18.42863 18.42863 18.42862 18.42861 18.42861 18.42860 18.42860 18.42859 18.42859 18.42859 18.42859 18.42858 18.42858 18.42858 18.42858 18.42858 18.42858 18.42858 18.42858 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857 18.42857