How to resolve the algorithm Sub-unit squares step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sub-unit squares step by step in the Nim programming language
Table of Contents
Problem Statement
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.
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sub-unit squares step by step in the Nim programming language
Source code in the nim programming language
import std/[algorithm, math, strutils]
func digits(n: Positive): seq[int] =
## Return the sequence of digits of "n".
var n = n.Natural
while n != 0:
result.add n mod 10
n = n div 10
result.reverse()
func toInt(digits: seq[int]): int =
## Convert a sequence of digits to an integer.
for d in digits:
result = 10 * result + d
func isSquare(n: int): bool =
## Return true if "n" is square.
let r = sqrt(n.toFloat).int
result = r * r == n
echo "First eight sub-unit squares:"
echo 1
var n = 0
var count = 1
while count < 8:
inc n, 5
block Check:
var digs = digits(n * n)
for d in digs.mitems:
if d == 9: break Check
inc d
let s = digs.toInt
if s.isSquare:
inc count
echo s
You may also check:How to resolve the algorithm Bell numbers step by step in the C programming language
You may also check:How to resolve the algorithm Search in paragraph's text step by step in the Phix programming language
You may also check:How to resolve the algorithm Write entire file step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Motzkin numbers step by step in the RPL programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the AppleScript programming language