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.
- 950
- 1
- 5
- 16
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.
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.