How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Haskell programming language

Table of Contents

Problem Statement

Calculate the sequence where each term an is the smallest natural number greater than the previous term, that has exactly n divisors.

Show here, on this page, at least the first 15 terms of the sequence.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Haskell programming language

The given Haskell code generates the sequence A069654 from the the OEIS, which is defined as the smallest number that has exactly k divisors, for each k > 0.

Breaking Down the Code:

  1. Importing necessary module:

    import Text.Printf (printf)

    This line imports the printf function from the Text.Printf module for formatted output.

  2. Defining the Sequence:

    sequence_A069654 :: [(Int,Int)]
    sequence_A069654 = go 1 $ (,) <*> countDivisors <$> [1..]
    • sequence_A069654 is the main function for generating the sequence. It returns a list of tuples [(Int,Int)], where the first integer represents the number of divisors (k) and the second integer represents the smallest number that has exactly k divisors.
    • go is a helper function used to construct the sequence recursively. It takes two arguments: t (the current number of divisors) and a list of tuples ((n,c):xs).
    • (,) <*> countDivisors <$> [1..] is a list comprehension that applies the countDivisors function to each element of the list [1..], and then combines the result with the tuple constructor (,). This creates a list of tuples [(1,countDivisors 1),(2,countDivisors 2),...].
  3. go Function:

    where go t ((n,c):xs) | c == t    = (t,n):go (succ t) xs
                         | otherwise = go t xs
    • go function recursively constructs the sequence.
    • It checks if the c (count of divisors) for the current element (n,c) is equal to the target count t. If so, it prepends the current tuple (t,n) to the result and increments t for the next iteration, effectively extending the sequence with the smallest number that has t divisors.
    • If the c is not equal to t, it simply continues the recursion with the same t and the remaining list xs.
  4. countDivisors Function:

    countDivisors n = foldr f 0 [1..floor $ sqrt $ realToFrac n]
    • countDivisors function calculates the number of divisors for a given integer n.
    • It uses a fold (foldr) with an accumulator r initialized to 0 and a list of numbers from 1 to the floor of the square root of n.
    • The fold function f checks if n is divisible by the current number x. If it is, f increments r by 2 if n and x are not equal, and by 1 if they are equal (i.e., n is a perfect square).
  5. main Function:

    main :: IO ()
    main = mapM_ (uncurry $ printf "a(%2d)=%5d\n") $ take 15 sequence_A069654
    • main function is the entry point of the program.
    • It uses mapM_ to apply the formatted output function printf to each tuple in the first 15 elements of the sequence_A069654 sequence.
    • The output is in the format a(k)=n, where k is the number of divisors and n is the smallest number that has exactly k divisors.

Source code in the haskell programming language

import Text.Printf (printf)

sequence_A069654 :: [(Int,Int)]
sequence_A069654 = go 1 $ (,) <*> countDivisors <$> [1..]
 where go t ((n,c):xs) | c == t    = (t,n):go (succ t) xs
                       | otherwise = go t xs
       countDivisors n = foldr f 0 [1..floor $ sqrt $ realToFrac n]
        where f x r | n `mod` x == 0 = if n `div` x == x then r+1 else r+2
                    | otherwise      = r

main :: IO ()
main = mapM_ (uncurry $ printf "a(%2d)=%5d\n") $ take 15 sequence_A069654


  

You may also check:How to resolve the algorithm N-queens problem step by step in the Heron programming language
You may also check:How to resolve the algorithm Sum to 100 step by step in the J programming language
You may also check:How to resolve the algorithm Combinations step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Fraction reduction step by step in the Go programming language