Primes whose first and last number is 3: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 561:
 
Found 2,251 primes under 1,000,000 which begin and end with 3.
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">
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
 
condition :: Int -> Bool
condition n = isPrime n && head numstr == '3' && last numstr == '3'
where
numstr :: String
numstr = show n
 
solution :: [Int]
solution = filter condition [1..3999]
 
main :: IO ( )
main = do
print solution
putStrLn ( "There are " ++ ( show $ length $ filter condition [1..999999]
) ++ " 3 x 3 primes below 1000000!" )
</syntaxhighlight>
{{out}}
<pre>
[3,313,353,373,383,3023,3083,3163,3203,3253,3313,3323,3343,3373,3413,3433,3463,3533,3583,3593,3613,3623,3643,3673,3733,3793,3803,3823,3833,3853,3863,3923,3943]
There are 2251 3 x 3 primes below 1000000!
</pre>
 
258

edits