Skip to main content
Code Review

Return to Answer

improve English
Source Link

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) -- https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile (< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number withof humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) -- https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile (< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number with humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) -- https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile (< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number of humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

Fixed comment in source code
Source Link

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) //-- https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile (< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number with humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) //https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile (< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number with humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) -- https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile (< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number with humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

minor fix
Source Link

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) //https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile(< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number with humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) //https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile(< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number with humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

Based on this Wikipedia article, here's a function to calculate the first n perfect numbers:

Import Data.Numbers.Primes(primes, isPrime) //https://hackage.haskell.org/package/primes-0.2.1.0/docs/src/Data-Numbers-Primes.html
-- A perfect number is a number of the form 2p×ばつ (2p − 1) 
-- where 2p − 1 is a Mersenne prime (http://en.wikipedia.org/wiki/List_of_perfect_numbers)
firstPerfectNumbers :: Int -> [Integer]
firstPerfectNumbers n = 
 take n [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

This function returns the first 8 numbers in a second, but slows down after that.

Your original upUntil function could be implemented like this:

perfectNumbersUpTo :: Integer -> [Integer]
perfectNumbersUpTo n = 
 takeWhile(< n) [2 ^ (x - 1) * (2 ^ x - 1) | x <- primes, isPrime(2 ^ x - 1)]

But of course, seeing as there are only 35 perfect numbers with less than 1,000,000 digits and 48 known numbers, if I wanted to use a perfect number in a program I would probably use a precomputed table.

Also, if you aren't using your upUntil function to try to find new perfect numbers, or if your program isn't critically dependent on a perfect number with humongous size, then your isPerfect function would probably benefit from this:

isPerfect n | odd n = False

Because it's not known whether odd perfect numbers exist.

Add perfectNumbersUpTo
Source Link
Loading
Source Link
Loading
lang-hs

AltStyle によって変換されたページ (->オリジナル) /