Find adjacent primes which differ by a square integer: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
No edit summary |
||
Line 536: | Line 536: | ||
240 IF Q = 1 THEN P2 = P: P = T: RETURN |
240 IF Q = 1 THEN P2 = P: P = T: RETURN |
||
250 GOTO 220</syntaxhighlight> |
250 GOTO 220</syntaxhighlight> |
||
=={{header|Haskell}}== |
|||
<syntaxhighlight lang="haskell"> |
|||
import Data.List.Split ( divvy ) |
|||
isSquare :: Int -> Bool |
|||
isSquare n = (snd $ properFraction $ sqrt $ fromIntegral n) == 0.0 |
|||
isPrime :: Int -> Bool |
|||
isPrime n |
|||
|n == 2 = True |
|||
|n == 1 = False |
|||
|otherwise = null $ filter (\i -> mod n i == 0 ) [2 .. root] |
|||
where |
|||
root :: Int |
|||
root = floor $ sqrt $ fromIntegral n |
|||
solution :: [[Int]] |
|||
solution = filter (\li -> isSquare (last li - head li ) && |
|||
( last li - head li ) > 36 ) $ divvy 2 1 $ filter isPrime [2..1000000] |
|||
printResultLine :: [Int] -> String |
|||
printResultLine list = show ( last list ) ++ " - " ++ ( show $ head list ) |
|||
++ " = " ++ ( show ( last list - head list )) |
|||
main :: IO ( ) |
|||
main = do |
|||
let resultPairs = solution |
|||
mapM_ (\li -> putStrLn $ printResultLine li ) resultPairs</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
89753 - 89689 = 64 |
|||
107441 - 107377 = 64 |
|||
288647 - 288583 = 64 |
|||
368021 - 367957 = 64 |
|||
381167 - 381103 = 64 |
|||
396833 - 396733 = 100 |
|||
400823 - 400759 = 64 |
|||
445427 - 445363 = 64 |
|||
623171 - 623107 = 64 |
|||
625763 - 625699 = 64 |
|||
637067 - 637003 = 64 |
|||
710777 - 710713 = 64 |
|||
725273 - 725209 = 64 |
|||
779477 - 779413 = 64 |
|||
801947 - 801883 = 64 |
|||
803813 - 803749 = 64 |
|||
821741 - 821677 = 64 |
|||
832583 - 832519 = 64 |
|||
838349 - 838249 = 100 |
|||
844841 - 844777 = 64 |
|||
883871 - 883807 = 64 |
|||
912167 - 912103 = 64 |
|||
919511 - 919447 = 64 |
|||
954827 - 954763 = 64 |
|||
981887 - 981823 = 64 |
|||
997877 - 997813 = 64 |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |