Minimal steps down to 1: Difference between revisions
m
syntax highlighting fixup automation
(Minimal steps down to 1 in FreeBASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 1:
{{task}}
Given:
Line 51 ⟶ 49:
{{trans|Python: Tabulated}}
<
Set[Int] divs, subs
[Int] table
Line 102 ⟶ 100:
V mx = max(mintab.table[1..])
V ans = enumerate(mintab.table).filter((n, steps) -> steps == @mx).map((n, steps) -> n)
print(‘ Taking ’mx‘ steps is/are the ’ans.len‘ numbers: ’ans.map(n -> String(n)).join(‘, ’))</
{{out}}
Line 150 ⟶ 148:
=={{header|C sharp}}==
{{works with|C sharp|7}}
<
using System.Collections.Generic;
using System.Linq;
Line 223 ⟶ 221:
private static string Delimit<T>(this IEnumerable<T> source) => string.Join(", ", source);
}</
{{out}}
<pre>
Line 256 ⟶ 254:
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<
Dim Shared As Integer Subtractor '1 or 2
Dim Shared As Integer Ns(20000), Ops(20000), MinNs(20000), MinOps(20000)
Line 321 ⟶ 319:
ShowCount(2000) '4.
ShowCount(20000) '4a.
Sleep</
{{out}}
<pre>Same as XPL0 entry.</pre>
=={{header|Go}}==
<
import (
Line 419 ⟶ 417:
fmt.Println()
}
}</
{{out}}
Line 463 ⟶ 461:
=={{header|Haskell}}==
<
import Data.List
import Data.Ord
Line 506 ⟶ 504:
(s, k) <- steps >>= run n
let (m, ss) = go k
return (m+1, s:ss)</
<pre>λ> minSteps [Div 2, Div 3, Sub 1] 123
Line 516 ⟶ 514:
The task
<
showSteps = foldl go . show
where
Line 535 ⟶ 533:
head $ groupBy ((==) `on` (fst.snd)) $
sortOn (negate . fst . snd) $
zip [1..] (minSteps steps <$> range)</
<pre>λ> task1 [Div 2, Div 3, Sub 1]
Line 585 ⟶ 583:
Implementation:
<
~.((#~ 1<:]),y-/m),(#~ (=<.)),y%/n
}}
Line 605 ⟶ 603:
;@((<":y),,)"2((' -/'{~{.);":@{:)"1}:"2}:"1 paths
}}
</syntaxhighlight>
Counting the steps is rather trivial -- we build a step function which subtracts the possible subtractors (discarding numbers which are too small) and divides by the possible divisors (discarding numbers which are not integers), then we iterate on that until we find a 1.
Line 613 ⟶ 611:
Task examples:
<
for_j. 1+i.10 do.
echo rplc&(' 1 steps';' 1 step')j,&":': ',(_1+#x steps y j),&":' steps.'
Line 726 ⟶ 724:
considering positive integers up to 20000
24 steps: 19681
</syntaxhighlight>
=={{header|Java}}==
Line 732 ⟶ 730:
Algorithm works with any function that supports the <code>Function</code> interface in the code below.
<
import java.util.ArrayList;
import java.util.HashMap;
Line 947 ⟶ 945:
}
</syntaxhighlight>
{{out}}
Line 1,011 ⟶ 1,009:
Implemented as a generic solution for any functions acting on an integer and taking any range of second arguments, with the goal solution also specifiable. To do so generically, it is also necessary to specify a failure condition, which in the example is falling below 1.
<
struct Action{T}
Line 1,102 ⟶ 1,100:
empty!(memoized)
teststeps(1, failed, actions2, [2000, 20000, 50000])
</
<pre>
With goal 1, divisors [2, 3], subtractors [1]:
Line 1,174 ⟶ 1,172:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
ClearAll[MinimalStepToOne, MinimalStepToOneHelper]
MinimalStepToOne[n_Integer] := Module[{res},
Line 1,382 ⟶ 1,380:
a = Last[SortBy[GatherBy[allsols, Last], First /* Last]];
{a[[1, 2]], a[[All, 1]]}</
{{out}}
<pre>1: (0 steps) 1
Line 1,411 ⟶ 1,409:
We use two recursive functions, the first one to find the minimal length sequences, the second one to find the minimal number of steps. For both, we use memoization. The program takes about 10 ms to execute.
<
type
Line 1,531 ⟶ 1,529:
run(divisors = [2, 3], subtractors = [1])
echo ""
run(divisors = [2, 3], subtractors = [2])</
{{out}}
Line 1,563 ⟶ 1,561:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Minimal_steps_down_to_1
Line 1,610 ⟶ 1,608:
}
return \@solve, \@maximal;
}</
{{out}}
<pre>
Line 1,646 ⟶ 1,644:
=={{header|Phix}}==
Using simple iterative (vs recursive) memoisation. To make things a bit more interesting it maintains separate caches for any&all {d,s} sets.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cache</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
Line 1,721 ⟶ 1,719:
<span style="color: #000000;">maxsteps</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">20000</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">maxsteps</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">50000</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 1,760 ⟶ 1,758:
Although the stretch goal could be achieved by changing the recursion limit, it does point out a possible issue with this type of solution. But then again, this solution may be more natural to some.
<
from functools import lru_cache
Line 1,815 ⟶ 1,813:
', '.join(str(n) for n in sorted(ans)))
#print(minrec._minrec.cache_info())
print()</
{{out}}
Line 1,859 ⟶ 1,857:
The table to solve for N contains all the results from 1 up to N. This is used in the solution.
<
"Tabulation, memoised minimised steps to 1"
Line 1,916 ⟶ 1,914:
ans = [n for n, steps in enumerate(table) if steps == mx]
print(' Taking', mx, f'steps is/are the {len(ans)} numbers:',
', '.join(str(n) for n in ans))</
{{out}}
Line 1,963 ⟶ 1,961:
{{works with|Rakudo|2019.11}}
<syntaxhighlight lang="raku"
for [2,3], 1, 2000,
Line 2,001 ⟶ 1,999:
}
}
}</
{{out}}
<pre>Divisors: [2, 3], subtract: 1
Line 2,060 ⟶ 2,058:
{{trans|Python}}
<
var table = Array(repeating: n + 2, count: n + 1)
var how = Array(repeating: [""], count: n + 2)
Line 2,114 ⟶ 2,112:
)
}
}</
{{out}}
Line 2,161 ⟶ 2,159:
{{trans|Go}}
{{libheader|Wren-fmt}}
<
var limit = 50000
Line 2,243 ⟶ 2,241:
}
System.print()
}</
{{out}}
Line 2,287 ⟶ 2,285:
=={{header|XPL0}}==
<
Subtractor; \1 or 2
char Ns(20000), Ops(20000), MinNs(20000), MinOps(20000);
Line 2,354 ⟶ 2,352:
ShowCount(2000); \4.
ShowCount(20_000); \4a.
]</
{{out}}
Line 2,385 ⟶ 2,383:
=={{header|zkl}}==
<
fcn buildCache(N,D,S){
minCache=Dictionary(1,T(1,"",0));
Line 2,402 ⟶ 2,400:
do(steps){ v,o,s := minCache[N]; ops.write(" ",o,"-->",N=v); }
return(steps,ops.close())
}</
<
buildCache(MAX,D,S);
Line 2,417 ⟶ 2,415:
S=T(2); buildCache(MAX,D,S);
}</
{{out}}
<pre style="height:45ex">
|