How to resolve the algorithm Minimal steps down to 1 step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Minimal steps down to 1 step by step in the Raku programming language
Table of Contents
Problem Statement
Given: The goal is find the minimum number of steps necessary to reduce N down to one. At any step, the number may be:
There may be many ways to reduce the initial N down to 1. Your program needs to:
No divisors, D. a single subtractor of 1. Single divisor of 2; single subtractor of 1: Divisors 2 and 3; subtractor 1: Using the possible divisors D, of 2 and 3; together with a possible subtractor S, of 1: Using the possible divisors D, of 2 and 3; together with a possible subtractor S, of 2:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Minimal steps down to 1 step by step in the Raku programming language
Source code in the raku programming language
use Lingua::EN::Numbers;
for [2,3], 1, 2000,
[2,3], 1, 50000,
[2,3], 2, 2000,
[2,3], 2, 50000
-> @div, $sub, $limit {
my %min = 1 => {:op(''), :v(1), :s(0)};
(2..$limit).map( -> $n {
my @ops;
@ops.push: ($n / $_, "/$_") if $n %% $_ for @div;
@ops.push: ($n - $sub, "-$sub") if $n > $sub;
my $op = @ops.min( {%min{.[0]}} );
%min{$n} = {:op($op[1]), :v($op[0]), :s(1 + %min{$op[0]})};
});
my $max = %min.max( {.value} ).value;
my @max = %min.grep( {.value. == $max} )».key.sort(+*);
if $limit == 2000 {
say "\nDivisors: {@div.perl}, subtract: $sub";
steps(1..10);
}
say "\nUp to {comma $limit} found {+@max} number{+@max == 1 ?? '' !! 's'} " ~
"that require{+@max == 1 ?? 's' !! ''} at least $max steps.";
steps(@max);
sub steps (*@list) {
for @list -> $m {
my @op;
my $n = $m;
while %min{$n} {
@op.push: "{%min{$n}}=>{%min{$n}}";
$n = %min{$n};
}
say "($m) {%min{$m}} steps: ", @op.join(', ');
}
}
}
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Raku programming language
You may also check:How to resolve the algorithm JSON step by step in the REBOL programming language
You may also check:How to resolve the algorithm Collections step by step in the VBA programming language
You may also check:How to resolve the algorithm Input loop step by step in the NodeJS programming language
You may also check:How to resolve the algorithm Last letter-first letter step by step in the Lua programming language