How to resolve the algorithm Sub-unit squares step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sub-unit squares step by step in the Rust 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 Rust programming language
Source code in the rust programming language
fn is_square_number( number : u64 ) -> bool {
let root : f64 = (number as f64).sqrt( ).floor( ) ;
(root as u64) * (root as u64) == number
}
fn is_subunit_square( number : u64 ) -> bool {
let numberstring = number.to_string( ) ;
let numstring : &str = numberstring.as_str( ) ;
if numstring.contains( '0' ) {
return false ;
}
else {
if number < 10 {
is_square_number( number ) && is_square_number( number - 1 )
}
else {
let mut digits : Vec<u64> = Vec::new( ) ;
let mut num : u64 = number ;
while num != 0 {
digits.push( num % 10 ) ;
num /= 10 ;
}
for n in digits.iter_mut( ) {
*n -= 1 ;
}
let mut sum : u64 = 0 ;
let mut factor : u64 = 1 ;
for d in digits {
sum += d * factor ;
factor *= 10 ;
}
is_square_number( sum ) && is_square_number( number )
}
}
}
fn main() {
let mut solution : Vec<u64> = Vec::new( ) ;
let mut current : u64 = 1 ;
while solution.len( ) != 7 {
if is_subunit_square( current ) {
solution.push( current ) ;
}
current += 1 ;
}
println!("{:?}" , solution ) ;
}
You may also check:How to resolve the algorithm Reflection/List properties step by step in the Factor programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the Axe programming language
You may also check:How to resolve the algorithm Variable-length quantity step by step in the C# programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Sockets step by step in the Icon and Unicon programming language