How to resolve the algorithm Sub-unit squares step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Sub-unit squares step by step in the Haskell 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 Haskell programming language

This Haskell code defines two functions: isSquareNumber and isSubunitSquare, and then uses them to generate a list of the first 7 subunit square numbers in solution.

Here's a breakdown of the code:

  1. isSquareNumber checks if a given integer n is a perfect square. It does this by calculating the square root of n, rounding it down to the nearest integer using floor, and checking if the result squared is equal to n.

  2. isSubunitSquare checks if a given integer n is a subunit square. A subunit square is a number that is both a perfect square and has the property that its digits, when decremented by 1 and then converted back to digits, form another perfect square.

  3. The code first converts n to a string numstr using show.

  4. It then creates a new number subunitnum by mapping a function over the digits of numstr. This function decrements each digit by 1 and then converts it back to a digit using intToDigit.

  5. Finally, it checks if both n and subunitnum are perfect squares using isSquareNumber.

  6. solution generates a list of the first 7 subunit square numbers by filtering the list of all positive integers starting from 1 using isSubunitSquare and then taking the first 7 elements using take.

Source code in the haskell programming language

import Data.Char ( digitToInt , intToDigit )

isSquareNumber :: Int -> Bool 
isSquareNumber n = root * root == n
 where
  root :: Int
  root = floor $ sqrt $ fromIntegral n

isSubunitSquare :: Int -> Bool
isSubunitSquare n 
   |elem '0' numstr = False
   |otherwise = isSquareNumber n && isSquareNumber subunitnum
   where
    numstr :: String
    numstr = show n
    subunitnum :: Int
    subunitnum = read $ map (intToDigit . pred . digitToInt ) numstr 

solution :: [Int]
solution = take 7 $ filter isSubunitSquare [1,2..]


  

You may also check:How to resolve the algorithm Loops/Nested step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Loops/For step by step in the J programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the Maple programming language
You may also check:How to resolve the algorithm A+B step by step in the GUISS programming language
You may also check:How to resolve the algorithm System time step by step in the Maxima programming language