Minimal steps down to 1: Difference between revisions

m
syntax highlighting fixup automation
(Minimal steps down to 1 in FreeBASIC)
m (syntax highlighting fixup automation)
Line 1:
{{task}}
<!-- Happy Holidays :-) -->
 
 
Given:
Line 51 ⟶ 49:
{{trans|Python: Tabulated}}
 
<langsyntaxhighlight lang="11l">T Mintab
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(‘, ’))</langsyntaxhighlight>
 
{{out}}
Line 150 ⟶ 148:
=={{header|C sharp}}==
{{works with|C sharp|7}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 223 ⟶ 221:
 
private static string Delimit<T>(this IEnumerable<T> source) => string.Join(", ", source);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 256 ⟶ 254:
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<langsyntaxhighlight lang="freebasic">Dim Shared As Integer minPasos 'minimal number of steps to get to 1
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</langsyntaxhighlight>
{{out}}
<pre>Same as XPL0 entry.</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 419 ⟶ 417:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 463 ⟶ 461:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE DeriveFunctor #-}
import Data.List
import Data.Ord
Line 506 ⟶ 504:
(s, k) <- steps >>= run n
let (m, ss) = go k
return (m+1, s:ss)</langsyntaxhighlight>
 
<pre>λ> minSteps [Div 2, Div 3, Sub 1] 123
Line 516 ⟶ 514:
The task
 
<langsyntaxhighlight lang="haskell">showSteps :: Int -> [Step] -> String
showSteps = foldl go . show
where
Line 535 ⟶ 533:
head $ groupBy ((==) `on` (fst.snd)) $
sortOn (negate . fst . snd) $
zip [1..] (minSteps steps <$> range)</langsyntaxhighlight>
 
<pre>λ> task1 [Div 2, Div 3, Sub 1]
Line 585 ⟶ 583:
 
Implementation:
<langsyntaxhighlight Jlang="j">step=: {{
~.((#~ 1<:]),y-/m),(#~ (=<.)),y%/n
}}
Line 605 ⟶ 603:
;@((<":y),,)"2((' -/'{~{.);":@{:)"1}:"2}:"1 paths
}}
</syntaxhighlight>
</lang>
 
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:
 
<langsyntaxhighlight Jlang="j">taskA=: {{
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>
</lang>
 
=={{header|Java}}==
Line 732 ⟶ 730:
Algorithm works with any function that supports the <code>Function</code> interface in the code below.
 
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.HashMap;
Line 947 ⟶ 945:
 
}
</syntaxhighlight>
</lang>
 
{{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.
<langsyntaxhighlight lang="julia">import Base.print
struct Action{T}
Line 1,102 ⟶ 1,100:
empty!(memoized)
teststeps(1, failed, actions2, [2000, 20000, 50000])
</langsyntaxhighlight>{{out}}
<pre>
With goal 1, divisors [2, 3], subtractors [1]:
Line 1,174 ⟶ 1,172:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">$RecursionLimit = 3000;
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]]}</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight Nimlang="nim">import strformat, strutils, tables
 
type
Line 1,531 ⟶ 1,529:
run(divisors = [2, 3], subtractors = [1])
echo ""
run(divisors = [2, 3], subtractors = [2])</langsyntaxhighlight>
 
{{out}}
Line 1,563 ⟶ 1,561:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Minimal_steps_down_to_1
Line 1,610 ⟶ 1,608:
}
return \@solve, \@maximal;
}</langsyntaxhighlight>
{{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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{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.
 
<langsyntaxhighlight lang="python">
from functools import lru_cache
 
Line 1,815 ⟶ 1,813:
', '.join(str(n) for n in sorted(ans)))
#print(minrec._minrec.cache_info())
print()</langsyntaxhighlight>
 
{{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.
 
<langsyntaxhighlight lang="python">class Mintab():
"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))</langsyntaxhighlight>
 
{{out}}
Line 1,963 ⟶ 1,961:
{{works with|Rakudo|2019.11}}
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
for [2,3], 1, 2000,
Line 2,001 ⟶ 1,999:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Divisors: [2, 3], subtract: 1
Line 2,060 ⟶ 2,058:
{{trans|Python}}
 
<langsyntaxhighlight lang="swift">func minToOne(divs: [Int], subs: [Int], upTo n: Int) -> ([Int], [[String]]) {
var table = Array(repeating: n + 2, count: n + 1)
var how = Array(repeating: [""], count: n + 2)
Line 2,114 ⟶ 2,112:
)
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,161 ⟶ 2,159:
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var limit = 50000
Line 2,243 ⟶ 2,241:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 2,287 ⟶ 2,285:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int MinSteps, \minimal number of steps to get to 1
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.
]</langsyntaxhighlight>
 
{{out}}
Line 2,385 ⟶ 2,383:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">var minCache; // (val:(newVal,op,steps))
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())
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">MAX, D,S := 50_000, T(2,3), T(1);
buildCache(MAX,D,S);
 
Line 2,417 ⟶ 2,415:
 
S=T(2); buildCache(MAX,D,S);
}</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
10,333

edits