Jump to content

Brazilian numbers: Difference between revisions

Added Forth solution
(→‎{{header|ALGOL W}}: Added a note.)
(Added Forth solution)
Line 1,082:
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Forth}}==
<lang forth>: prime? ( n -- flag )
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
dup 3 mod 0= if 3 = exit then
5
begin
2dup dup * >=
while
2dup mod 0= if 2drop false exit then
2 +
2dup mod 0= if 2drop false exit then
4 +
repeat
2drop true ;
 
: same_digits? ( n b -- ? )
2dup mod >r
begin
tuck / swap
over 0 >
while
2dup mod r@ <> if
2drop rdrop false exit
then
repeat
2drop rdrop true ;
 
: brazilian? ( n -- ? )
dup 7 < if drop false exit then
dup 1 and 0= if drop true exit then
dup 1- 2 do
dup i same_digits? if
unloop drop true exit
then
loop
drop false ;
 
: next_prime ( n -- n )
begin 2 + dup prime? until ;
 
: print_brazilian ( n1 n2 -- )
>r 7
begin
r@ 0 >
while
dup brazilian? if
dup .
r> 1- >r
then
over 0= if
next_prime
else
over +
then
repeat
2drop rdrop cr ;
 
." First 20 Brazilian numbers:" cr
1 20 print_brazilian
cr
 
." First 20 odd Brazilian numbers:" cr
2 20 print_brazilian
cr
 
." First 20 prime Brazilian numbers:" cr
0 20 print_brazilian
 
bye</lang>
 
{{out}}
<pre>
First 20 Brazilian numbers:
7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33
 
First 20 odd Brazilian numbers:
7 13 15 21 27 31 33 35 39 43 45 51 55 57 63 65 69 73 75 77
 
First 20 prime Brazilian numbers:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
</pre>
 
=={{header|FreeBASIC}}==
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.