Sub-unit squares

From Rosetta Code
Revision as of 16:30, 22 September 2022 by Thundergnat (talk | contribs) (New draft task and Raku example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Sub-unit squares is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A sub-unit square is a square number (product of two identical non-negative integers) that remains a square after having a 1 subtracted from each digit in the square.


E.G.

The number 1 is a sub-unit square. 1 - 1 is 0, which is also a square, though it's kind-of a degenerate case.

The number 3136 is a sub-unit square. 3136 (56²) with unit 1 subtracted from each digit is 2025 (45²).


A sub-unit square cannot contain a digit zero (0) since zero minus one is negative one. Every known sub-unit square, with the exception of 1, ends with the digits 36.


Task
  • Find and display at least the first five sub-unit squares.


See also


Raku

First seven take about 5 seconds with this implementation. The eighth would take several hours at least.

my @upper = 1,|(1 .. *).map((* × 2)²);
my @lower = (^∞).map: *²;
my \R = [\+] 0, 1, 10, 100 … *;

my $l = 0;

.say for (gather {
    (^∞).map: -> $u {
        next if @upper[$u].contains: 0;
        my $chars = @upper[$u].chars;
        loop {
            $l++ and next if @upper[$u] - @lower[$l] > R[$chars];
            take @upper[$u] if (@upper[$u] - @lower[$l] == R[$chars]);
            last;
        }
    }
})[^7]
Output:
1
36
3136
24336
5973136
71526293136
318723477136