Sequence: nth number with exactly n divisors: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Fix Perl 6 -> Raku links) |
No edit summary |
||
Line 403: | Line 403: | ||
45 : 327184 |
45 : 327184 |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
<lang haskell>import Control.Monad (guard) |
|||
import Data.List (find, unfoldr) |
|||
import Math.NumberTheory.ArithmeticFunctions (divisorCount) |
|||
import Math.NumberTheory.Primes (Prime, nextPrime, |
|||
unPrime) |
|||
import Math.NumberTheory.Primes.Testing (isPrime) |
|||
calc :: Integer -> [(Integer, Integer)] |
|||
calc n = do |
|||
x <- [1..] |
|||
guard (even n || odd n && f x ^ 2 == x) |
|||
[(x, divisorCount x)] |
|||
where f n = floor (sqrt $ realToFrac n) |
|||
havingNthDivisors :: Integer -> [(Integer, Integer)] |
|||
havingNthDivisors n = filter ((==n) . snd) $ calc n |
|||
nths :: [(Integer, Integer)] |
|||
nths = do |
|||
n <- [1..30] :: [Integer] |
|||
if isPrime n then |
|||
pure (n, unPrime (nthPrime (fromIntegral n)) ^ (n - 1)) |
|||
else |
|||
pure (n, f n) |
|||
where |
|||
f n = fst (havingNthDivisors n !! pred (fromIntegral n)) |
|||
nthPrime n = toEnum n :: Prime Integer |
|||
main :: IO () |
|||
main = mapM_ print nths</lang> |
|||
{{out}} |
|||
<pre>(1,1) |
|||
(2,3) |
|||
(3,25) |
|||
(4,14) |
|||
(5,14641) |
|||
(6,44) |
|||
(7,24137569) |
|||
(8,70) |
|||
(9,1089) |
|||
(10,405) |
|||
(11,819628286980801) |
|||
(12,160) |
|||
(13,22563490300366186081) |
|||
(14,2752) |
|||
(15,9801) |
|||
(16,462) |
|||
(17,21559177407076402401757871041) |
|||
(18,1044) |
|||
(19,740195513856780056217081017732809) |
|||
(20,1520) |
|||
(21,141376) |
|||
(22,84992) |
|||
(23,1658509762573818415340429240403156732495289) |
|||
(24,1170) |
|||
(25,52200625) |
|||
(26,421888) |
|||
(27,52900) |
|||
(28,9152) |
|||
(29,1116713952456127112240969687448211536647543601817400964721) |
|||
(30,6768)</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||