How to resolve the algorithm Sequence: nth number 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: nth number 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 nth that has 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: nth number with exactly n divisors step by step in the Haskell programming language

The provided Haskell code snippet calculates and prints a list of pairs of integers (n, d), where n is an integer between 1 and 35, and d is the n-th divisor of n.

The code first defines a function calc that takes an integer as an argument and returns a list of pairs of integers. Each pair in the list consists of an integer x and the number of divisors of x. The function calc uses the divisorCount function from the Math.NumberTheory.ArithmeticFunctions module to count the number of divisors of an integer.

The code then defines a function havingNthDivisors that takes an integer as an argument and returns a list of pairs of integers. Each pair in the list consists of an integer n and the number of divisors of n. The function havingNthDivisors uses the calc function to calculate the number of divisors of each integer n and then filters the list to only include the pairs where the number of divisors is equal to n.

The code then defines a function nths that returns a list of pairs of integers. Each pair in the list consists of an integer n and the n-th divisor of n. The function nths uses the isPrime function from the Math.NumberTheory.Primes.Testing module to check if an integer is prime. If an integer is prime, its n-th divisor is n itself. Otherwise, the n-th divisor of n is the first element of the list returned by the havingNthDivisors function applied to n.

The code then defines a function f that takes an integer as an argument and returns the n-th divisor of n. The function f is used in the nths function to calculate the n-th divisor of an integer.

The code then defines a function nthPrime that takes an integer as an argument and returns the n-th prime number. The function nthPrime uses the toEnum and unPrime functions from the Math.NumberTheory.Primes module to convert between an integer and a Prime value.

The code then defines the main function, which is the entry point of the program. The main function calls the mapM_ function from the Control.Monad module to print each pair in the list returned by the nths function.

Source code in the haskell programming language

import           Control.Monad                         (guard)
import           Math.NumberTheory.ArithmeticFunctions (divisorCount)
import           Math.NumberTheory.Primes              (Prime, unPrime)
import           Math.NumberTheory.Primes.Testing      (isPrime)

calc :: Integer -> [(Integer, Integer)]
calc n = do
  x <- [1..]
  guard (even n || odd n && f x == x)
  [(x, divisorCount x)]
 where f n = floor (sqrt $ realToFrac n) ^ 2

havingNthDivisors :: Integer -> [(Integer, Integer)]
havingNthDivisors n = filter ((==n) . snd) $ calc n

nths :: [(Integer, Integer)]
nths = do
  n <- [1..35] :: [Integer]
  if isPrime n then
    pure (n, nthPrime (fromIntegral n) ^ pred n)
  else
    pure (n, f n)
 where
  f n = fst (havingNthDivisors n !! pred (fromIntegral n))
  nthPrime n = unPrime (toEnum n :: Prime Integer)

main :: IO ()
main = mapM_ print nths


  

You may also check:How to resolve the algorithm Pentagram step by step in the REXX programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Priority queue step by step in the Component Pascal programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Arturo programming language
You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the Perl programming language