How to resolve the algorithm Numbers which are not the sum of distinct squares step by step in the Perl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Numbers which are not the sum of distinct squares step by step in the Perl programming language
Table of Contents
Problem Statement
Integer squares are the set of integers multiplied by themselves: 1 x 1 = 1, 2 × 2 = 4, 3 × 3 = 9, etc. ( 1, 4, 9, 16 ... ) Most positive integers can be generated as the sum of 1 or more distinct integer squares. Many can be generated in multiple ways: The number of positive integers that cannot be generated by any combination of distinct squares is in fact finite:
Find and show here, on this page, every positive integer than cannot be generated as the sum of distinct squares. Do not use magic numbers or pre-determined limits. Justify your answer mathematically.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numbers which are not the sum of distinct squares step by step in the Perl programming language
Source code in the perl programming language
use v5.36;
use List::Util <sum uniq none>;
use Algorithm::Combinatorics 'combinations';
my @squares = map { $_**2 } 0..100;
my $sq;
while (++$sq) {
my(@sums, @run);
for (1..$sq) {
push @sums, sum @squares[@$_] for combinations [1..$sq+1], $_
}
@sums = uniq sort { $a <=> $b } @sums;
for (@sums) {
push(@run, $_) and next unless @run;
if ($_ == $run[-1] + 1) { push @run, $_ } else { last if @run > $squares[$sq]; @run = () }
}
next unless @run;
for my $i (1..$run[-1]) {
push @res, $i if none { $i eq $_ } @sums
}
last if @run > $squares[$sq];
}
You may also check:How to resolve the algorithm Run-length encoding step by step in the SQL programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Brainf*** programming language
You may also check:How to resolve the algorithm Repunit primes step by step in the Go programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Arturo programming language
You may also check:How to resolve the algorithm Random numbers step by step in the F# programming language