Prime numbers which contain 123: Difference between revisions

add RPL
No edit summary
(add RPL)
Line 1,089:
done...
</pre>
 
=={{header|RPL}}==
===Brute force===
≪ ALOG → max
≪ { } 1123
'''WHILE''' DUP max < '''REPEAT'''
NEXTPRIME
'''IF''' DUP →STR "123" POS '''THEN''' SWAP OVER + SWAP '''END'''
'''END''' NIP
≫ ≫ '<span style="color:blue">TASK</span>’ STO
{{out}}
<pre>
1: { 1123 1231 1237 8123 11239 12301 12323 12329 12343 12347 12373 12377 12379 12391 17123 20123 22123 28123 29123 31123 31231 31237 34123 37123 40123 41231 41233 44123 47123 49123 50123 51239 56123 59123 61231 64123 65123 70123 71233 71237 76123 81233 81239 89123 91237 98123 }
</pre>
Runs in 12 minutes 45 seconds on a HP-50g
 
===Lexicographic approach===
 
{| class="wikitable"
! RPL code
! Comment
|-
|
3 - DUP ALOG 1 - { } → ndigits maxleft results
≪ 0
'''DO'''
DUP DUP "" IFTE
ndigits OVER SIZE - ALOG 1 +
DUP 2 x 3 - '''FOR''' j
DUP "123" + j →STR TAIL + STR→
'''IF''' DUP ISPRIME? '''THEN''' 'results' STO+ '''ELSE''' DROP '''END'''
2 '''STEP'''
DROP 1 +
'''UNTIL''' DUP maxleft > '''END'''
DROP results
≫ ≫ '<span style="color:blue">PR123N</span>’ STO
|
<span style="color:blue">PR123N</span> ''( n_digits → { primes_with_123 } ) ''
n_digits -= 3 ; maxleft = 10^n_digits - 1
leftval = 0
loop
left = leftval == 0 ? "" ; leftval
tmp = 10^(n_digits - length(leftval)) + 1
for j = tmp to (tmp*2 - 3) step 2
n = left + "123" + j[2..]
add n to results if prime
next j
leftval++
end loop
display results (unsorted)
|}
4 <span style="color:blue">PR123N</span> 5 <span style="color:blue">PR123N</span> + SORT
Runs in 22 seconds on a HP-50g (35 times faster than brute force) - same results as above.
Finding the 451 primes under one million takes 4 minutes 30 seconds.
 
=={{header|Ruby}}==
Line 1,102 ⟶ 1,158:
451 123-primes below 1 million.
</pre>
 
 
=={{header|Sidef}}==
1,150

edits