How to resolve the algorithm Vampire number step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Vampire number step by step in the Haskell programming language

Table of Contents

Problem Statement

A vampire number is a natural decimal number with an even number of digits,   that can be factored into two integers. These two factors are called the   fangs,   and must have the following properties:

An example of a vampire number and its fangs:   1260 : (21, 60)

16758243290880, 24959017348650, 14593825548650

Note that a vampire number can have more than one pair of fangs.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Vampire number step by step in the Haskell programming language

The provided code defines a Haskell program that deals with vampire numbers and integer factorization. Here's a detailed explanation:

  1. Vampire Numbers:

    • vampires: This is an infinite list of vampire numbers, defined as integers whose factors can be rearranged to form the original number. The filter function is used to exclude non-vampire numbers.
  2. Factors of a Number:

    • fangs: This function takes an integer n and returns a list of pairs (x, y) such that x * y = n. It filters out odd numbers (as vampire numbers cannot have odd factors) and uses quot n to find the factors.

    • isfang: This is a helper function that checks if a pair (x, y) is valid for a vampire number. It ensures that the digits length of both x and y is the same as half of the original number's digits, excludes zero-ended factors, and checks if the digits of the original number can be rearranged to form both x and y.

  3. Integer Factors:

    • integerFactors: This function calculates the integer factors of a given number n. It does so by finding the lowest factors (from 1) and combining them with the cofactors (calculated by dividing n by the lowest factors) to cover all possibilities. It includes optimizations for perfect squares to avoid redundancy.
  4. Testing:

    • main: The main function is the entry point of the program. It prints the first 25 vampire numbers and also includes predefined vampire numbers that are known to exist. It uses the fangs function to find the factors of each vampire number and prints them as pairs.

In summary, this code finds vampire numbers, which are integers whose factors can be rearranged to form the original number, and calculates the integer factors of a given number. It includes optimizations for perfect squares and uses lambdas and function composition to concisely express its logic.

Source code in the haskell programming language

import Data.List (sort)
import Control.Arrow ((&&&))

-- VAMPIRE NUMBERS ------------------------------------------------------------
vampires :: [Int]
vampires = filter (not . null . fangs) [1 ..]

fangs :: Int -> [(Int, Int)]
fangs n
  | odd w = []
  | otherwise = ((,) <*> quot n) <$> filter isfang (integerFactors n)
  where
    ndigit :: Int -> Int
    ndigit 0 = 0
    ndigit n = 1 + ndigit (quot n 10)
    w = ndigit n
    xmin = 10 ^ (quot w 2 - 1)
    xmax = xmin * 10
    isfang x =
      x > xmin &&
      x < y &&
      y < xmax && -- same length
      (quot x 10 /= 0 || quot y 10 /= 0) && -- not zero-ended
      sort (show n) == sort (show x ++ show y)
      where
        y = quot n x

-- FACTORS --------------------------------------------------------------------
integerFactors :: Int -> [Int]
integerFactors n
  | n < 1 = []
  | otherwise =
    lows ++
    (quot n <$>
     (if intSquared == n -- A perfect square,
        then tail -- and cofactor of square root would be redundant.
        else id)
       (reverse lows))
  where
    (intSquared, lows) =
      (^ 2) &&& (filter ((0 ==) . rem n) . enumFromTo 1) $
      floor (sqrt $ fromIntegral n)

-- TEST -----------------------------------------------------------------------
main :: IO [()]
main =
  mapM
    (print . ((,) <*>) fangs)
    (take 25 vampires ++ [16758243290880, 24959017348650, 14593825548650])


  

You may also check:How to resolve the algorithm Combinations step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the C++ programming language
You may also check:How to resolve the algorithm Munching squares step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Clipper programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the Nim programming language