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

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 4 users not shown)
Line 562:
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>
 
=={{header|J}}==
<syntaxhighlight lang="j"> primes3x3=. 3 ;@; 10 <@(#~ 1&p:)@(30&* + 3 + 10 * i.)@^ i.
 
primes3x3 3
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
 
# primes3x3 5
2251</syntaxhighlight>
 
=={{header|jq}}==
Line 913 ⟶ 954:
Found 33 numbers
done...
</pre>
 
=={{header|RPL}}==
Uses a candidate numbers generator to speed up execution.
{{works with|HP|49}}
≪ { } 3
'''WHILE''' DUP 4000 < '''REPEAT'''
'''IF''' DUP ISPRIME? '''THEN''' SWAP OVER + SWAP '''END'''
10 +
'''IF''' DUP MANT IP 3 ≠ '''THEN''' XPON 1 + ALOG 3 * 3 + '''END'''
'''END''' DROP
≫ '<span style="color:blue">33PRIMES</span>' STO
{{out}}
<pre>
1: { 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 }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
puts "Primes below #{n=4000} which start and end with #{d=3}: "
p Prime.each(n).select{|pr| p_d = pr.digits; p_d.first == d && p_d.last == d}
 
print "\nCount of primes below #{n=1_000_000} which start and en with #{d=3}: "
puts Prime.each(n).count{|pr| p_d = pr.digits; p_d.first == d && p_d.last == d}
</syntaxhighlight>
{{out}}
<pre>Primes below 4000 which start and end with 3:
[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]
 
Count of primes below 1000000 which start and en with 3: 2251
</pre>
 
Line 958 ⟶ 1,030:
{{libheader|Wren-math}}
{{libheader|Wren-iterate}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
 
===Basic task===
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./iterate" for Stepped
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
var primes = []
Line 972 ⟶ 1,042:
}
System.print("Primes under 4,000 which begin and end in 3:")
for (chunk in Lst.chunks(primes, 11)) Fmt.printtprint("$,5d", chunkprimes, 11)
System.print("\nFound %(primes.count) such primes.")</syntaxhighlight>
 
Line 987 ⟶ 1,057:
===More general===
This version deals with primes (in base 10) beginning and ending with any specified digit and with up to a given number of digits.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./iterate" for Stepped
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
var getQualifyingPrimes = Fn.new { |x, d|
Line 1,012 ⟶ 1,081:
var len = d + ((d-1)/3).floor
Fmt.print("Primes under $,%(len)d which begin and end in $d:", 10.pow(d), x)
for (chunk in Lst.chunks(primes, 10)) Fmt.printtprint("$,%(len)d", chunkprimes, 10)
System.print("\nFound %(primes.count) such primes.\n")
}
9,476

edits