Juggler sequence: Difference between revisions
m
syntax highlighting fixup automation
(Corrected error in task description, see talk page.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 49:
{{trans|Nim}}
<
V a = Int64(n)
V r_count = 0
Line 67:
L(n) 20..39
V (l, h, i) = juggler(n)
print(f:‘{n} {l:2} {h:14} {i}’)</
{{out}}
Line 97:
{{trans|Python}}
<
assert(x >= 0)
Line 133:
L(k) [113, 173, 193, 2183, 11229, 15065]
juggler(k)</
{{out}}
Line 168:
=={{header|Ada}}==
<
with Ada.Numerics.Generic_Elementary_Functions;
Line 245:
begin
Put_Table;
end Juggler;</
{{out}}
<pre>
Line 275:
===Core language===
Keeping within AppleScript's usable number range:
<
script o
property sequence : {n}
Line 322:
end task
task()</
{{output}}
<
21: l[n] = 9, h[n] = 140, i[n] = 4
22: l[n] = 3, h[n] = 22, i[n] = 0
Line 344:
37: l[n] = 17, h[n] = 24906114455136, i[n] = 8
38: l[n] = 3, h[n] = 38, i[n] = 0
39: l[n] = 14, h[n] = 233046, i[n] = 3"</
===Shell script===
One of AppleScript's main roles is telling other software to do things. This includes Unix executables, many of which come with the system. In the following, the 'do shell script' command feeds a script to the Bash shell, which script itself contains code to be passed to and executed by the "bc" executable. It's essentially a script within a script within a script. The text returned from "bc", which can handle larger numbers than core AppleScript, contains lines which are just the zeros returned by the 'juggler' function, so these are stripped out using "sed". The 'do shell script' command is supplied by the StandardAdditions OSAX which comes with the system as a standard AppleScript extension. So ironically, there's not a single command from the core language in the following code. But it's legitimate AppleScript and the input and output are both AppleScript text objects.
<
define juggler(n) {
#auto temp,i,max,pos
Line 372:
juggler(30817); # Another 191 to here.
# juggler(48443) produced no result after running all night.
' | bc | sed -n '/^0$/ !p;'"</
{{output}}
<
21: l[n] = 9, h[n] = 140, i[n] = 4
22: l[n] = 3, h[n] = 22, i[n] = 0
Line 403:
15065: l[n] = 66, d[n] = 11723, i[n] = 25
15845: l[n] = 139, d[n] = 23889, i[n] = 43
30817: l[n] = 93, d[n] = 45391, i[n] = 39"</
=={{header|BQN}}==
<
Step ← ⌊⊢⋆(0.5 + 2|⊢)
¯1‿0‿0 + 3↑{
Line 414:
}
>⟨"NLIH"⟩ ∾ (⊢∾Juggle)¨ 20+↕20</
{{out}}
<Pre>┌─
Line 442:
{{trans|Go}}
{{libheader|GMP}}
<
#include <iomanip>
#include <iostream>
Line 490:
<< '\n';
}
}</
{{out}}
Line 541:
=={{header|F_Sharp|F#}}==
This task uses [[Isqrt_(integer_square_root)_of_X#F.23]]
<
// Juggler sequence. Nigel Galloway: August 19th., 2021
let J n=Seq.unfold(fun(n,i,g,l)->if n=1I then None else let e=match n.IsEven with true->Isqrt n |_->Isqrt(n**3) in Some((i,g,l),if e>i then (e,e,l+1,l+1) else (e,i,g,l+1)))(n,n,0,0)|>Seq.last
printfn " n l[n] i[n] h[n]\n___________________"; [20I..39I]|>Seq.iter(fun n->let i,g,l=J n in printfn $"%d{int n}%5d{l+1}%5d{g} %A{i}")
printfn " n l[n] i[n] d[n]\n________________________"; [113I;173I;193I;2183I;11229I;15065I;15845I;30817I]|>Seq.iter(fun n->let i,g,l=J n in printfn $"%8d{int n}%5d{l+1}%5d{g} %d{(bigint.Log10>>int>>(+)1) i}")
</syntaxhighlight>
{{out}}
<pre>
Line 586:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
math.extras math.functions.integer-logs math.order math.ranges
sequences strings tools.memory.private ;
Line 623:
{ 113 173 193 2183 11229 15065 15845 30817 }
[ integer-log10 1 + ] "d[n]" juggler.</
{{out}}
<pre>
Line 670:
The next four record holders for the largest term (see talk page), are also doable but increased the overall time to nearly 24 minutes on my machine.
<
import (
Line 730:
fmt.Printf("%11s %3d %3d %s\n", cn, count, maxCount, rcu.Commatize(digits))
}
}</
{{out}}
Line 783:
Integer square root is computed as in [[Isqrt_(integer_square_root)_of_X#Haskell]]
<
import Data.List
Line 805:
mapM_ task [20..39]
putStrLn "\nTough guys\n"
mapM_ task [ 113, 173, 193, 2183, 11229, 15065, 15845, 30817 ]</
<pre>n = 20 length = 3 maximal value at = 0 (20)
Line 841:
=={{header|J}}==
<
Instead, we take the square root of either the even number or the third power of the odd number:
<
Task examples:
<
task=: {{
Line 874:
37: l: 18, h: 24906114455136, i:8
38: l: 4, h: 38, i:0
39: l: 15, h: 233046, i:3</
Sadly, J's extended precision implementation is antiquated (slow), hopefully that will be fixed before too long.
Still, some of the stretch exercises can be computed quickly:
<
echo '%d: l: %d, d: %d, i:%d' sprintf y;(#;#@":@(>./);]i.>./)jugx^:a: y
}}
Line 888:
193: l: 74, d: 271, i:47
2183: l: 73, d: 5929, i:32
11229: l: 102, d: 8201, i:54</
=={{header|jq}}==
Line 896:
The following jq program uses `idivide/1`, `isqrt/0`, and `lpad/1` as defined at
[[Isqrt_(integer_square_root)_of_X#jq]].
<
. as $n
| if $n < 1 then "juggler starting value must be a positive integer." | error
Line 937:
| fmt(6; 6; 6; 8) );
task1, "", task2</
{{out}}
<pre>
Line 976:
=={{header|Julia}}==
<
function juggler(k, countdig=true, maxiters=20000)
Line 1,000:
2264915, 5812827])
@time juggler(7110201)
</
<pre>
n l(n) i(n) h(n) or d(n)
Line 1,043:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
stats[n_Integer] :=
Line 1,052:
{TableForm[Table[stats@n, {n, 20, 39}],
TableHeadings -> {None, {"n", "length", "max", "max pos"}}]</
{{out}}<pre>
Line 1,080:
=={{header|Nim}}==
Using only standard library, so limited to values of <code>n</code> less than 40.
<
func juggler(n: Positive): tuple[count: int; max: uint64; maxIdx: int] =
Line 1,098:
for n in 20..39:
let (l, h, i) = juggler(n)
echo &"{n} {l:2} {h:14} {i}"</
{{out}}
Line 1,125:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Juggler_sequence
Line 1,156:
printf "%8d %4d %3d d(n) = %d digits\n", $i, $count, $at, length $max;
}
}</
{{out}}
<pre>
Line 1,200:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/juggler.htm here].
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,251:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
<pre>
Line 1,301:
=={{header|Python}}==
Slowed to a crawl at n of 1267909, so did not run for larger n.
<
def juggler(k, countdig=True, maxiters=1000):
Line 1,322:
for k in [113, 173, 193, 2183, 11229, 15065, 15845, 30817, 48443, 275485, 1267909]:
juggler(k)
</
<pre>
n l(n) i(n) h(n) or d(n)
Line 1,361:
=={{header|Quackery}}==
<
over size -
space swap of
Line 1,395:
15 recho 2 recho cr ] is stats ( n --> )
20 times [ i^ 20 + stats ]</
{{out}}
Line 1,423:
Reaches 30817 fairly quickly but later values suck up enough memory that it starts thrashing the disk cache and performance drops off a cliff (on my system). Killed it after 10 minutes and capped list at 30817. Could rewrite to not try to hold entire sequence in memory at once, but probably not worth it. If you want sheer numeric calculation performance, Raku is probably not where it's at.
<syntaxhighlight lang="raku"
sub juggler (Int $n where * > 0) { $n, { $_ +& 1 ?? .³.&isqrt !! .&isqrt } … 1 }
Line 1,449:
printf "%10s %4d %4d %10s %6.2f seconds\n", .&comma, +@j-1, @j.first(* == $max, :k),
$max.chars.&comma, (now - $start);
}</
{{out}}
<pre> n l[n] i[n] h[n]
Line 1,490:
Another optimization was to reduce the number of digits after the sqrt was calculated.
<
numeric digits 20 /*define the number of decimal digits. */
parse arg LO HI list /*obtain optional arguments from the CL*/
Line 1,546:
if z>mx then do; mx= z; imx= j; end /*found a new max; set MX; set IMX. */
#= z
end /*j*/; return j</
{{out|output|text= when using the inputs: <tt> , , 113 173 193 2183 11229 15065 30817 48443 </tt>}}
<pre>
Line 1,587:
=={{header|Ruby}}==
<
(20..39).chain([113, 173, 193, 2183, 11229, 15065, 15845, 30817, 48443, 275485, 1267909, 2264915]).each do |k|
Line 1,603:
end
end
</syntaxhighlight>
{{out}}
<pre>20: l[n] = 3, h[n] = 20, i[n] = 0
Line 1,645:
{{libheader|Wren-big}}
This took just over 17 minutes to reach n = 30,817 on my machine and I gave up after that.
<
import "/big" for BigInt
Line 1,684:
var res = juggler.call(n)
Fmt.print("$,6d $3d $3d $,6i", n, res[0], res[1], res[3])
}</
{{out}}
Line 1,727:
{{libheader|Wren-gmp}}
Massive speed-up, of course, when one brings in GMP. Now takes about 1 minute 48 seconds to reach 7,110,201 which is not much slower than Go on the same machine!
<
import "./gmp" for Mpz
Line 1,772:
var res = juggler.call(n)
Fmt.print("$,9d $3d $3d $,i", n, res[0], res[1], res[3])
}</
{{out}}
|