Brazilian numbers: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 4,399: | Line 4,399: | ||
</pre> |
</pre> |
||
The point of utilizing a sieve is that it caches or memoizes the results. Since we are going through a long sequence of possible Brazilian numbers, it pays off to check the prime factoring in an efficient way, rather than one at a time. |
The point of utilizing a sieve is that it caches or memoizes the results. Since we are going through a long sequence of possible Brazilian numbers, it pays off to check the prime factoring in an efficient way, rather than one at a time. |
||
=={{header|Vlang}}== |
|||
{{trans|Go}} |
|||
<lang vlang>fn same_digits(nn int, b int) bool { |
|||
f := nn % b |
|||
mut n := nn/b |
|||
for n > 0 { |
|||
if n%b != f { |
|||
return false |
|||
} |
|||
n /= b |
|||
} |
|||
return true |
|||
} |
|||
fn is_brazilian(n int) bool { |
|||
if n < 7 { |
|||
return false |
|||
} |
|||
if n%2 == 0 && n >= 8 { |
|||
return true |
|||
} |
|||
for b in 2..n-1 { |
|||
if same_digits(n, b) { |
|||
return true |
|||
} |
|||
} |
|||
return false |
|||
} |
|||
fn is_prime(n int) bool { |
|||
match true { |
|||
n < 2 { |
|||
return false |
|||
} |
|||
n%2 == 0 { |
|||
return n == 2 |
|||
} |
|||
n%3 == 0 { |
|||
return n == 3 |
|||
} |
|||
else { |
|||
mut d := 5 |
|||
for d*d <= n { |
|||
if n%d == 0 { |
|||
return false |
|||
} |
|||
d += 2 |
|||
if n%d == 0 { |
|||
return false |
|||
} |
|||
d += 4 |
|||
} |
|||
return true |
|||
} |
|||
} |
|||
} |
|||
fn main() { |
|||
kinds := [" ", " odd ", " prime "] |
|||
for kind in kinds { |
|||
println("First 20${kind}Brazilian numbers:") |
|||
mut c := 0 |
|||
mut n := 7 |
|||
for { |
|||
if is_brazilian(n) { |
|||
print("$n ") |
|||
c++ |
|||
if c == 20 { |
|||
println("\n") |
|||
break |
|||
} |
|||
} |
|||
match kind { |
|||
" " { |
|||
n++ |
|||
} |
|||
" odd " { |
|||
n += 2 |
|||
} |
|||
" prime "{ |
|||
for { |
|||
n += 2 |
|||
if is_prime(n) { |
|||
break |
|||
} |
|||
} |
|||
} |
|||
else{} |
|||
} |
|||
} |
|||
} |
|||
mut n := 7 |
|||
for c := 0; c < 100000; n++ { |
|||
if is_brazilian(n) { |
|||
c++ |
|||
} |
|||
} |
|||
println("The 100,000th Brazilian number: ${n-1}") |
|||
}</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 |
|||
The 100,000th Brazilian number: 110468 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |