Brazilian numbers: Difference between revisions

m
(Added solution for Action!)
 
(37 intermediate revisions by 17 users not shown)
Line 38:
:* '''[[oeis:A085104|OEIS:A085104 - Prime Brazilian numbers]]'''
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F isPrime(n)
I n % 2 == 0
R n == 2
Line 91 ⟶ 90:
printList(‘First 20 Brazilian numbers:’, n -> 1B)
printList(‘First 20 odd Brazilian numbers:’, n -> (n [&] 1) != 0)
printList(‘First 20 prime Brazilian numbers:’, n -> isPrime(n))</langsyntaxhighlight>
 
{{out}}
Line 105 ⟶ 104:
 
</pre>
 
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC SameDigits(INT x,b)
Line 178 ⟶ 176:
PutE() PutE()
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Brazilian_numbers.png Screenshot from Atari 8-bit computer]
Line 191 ⟶ 189:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
</pre>
 
=={{header|ALGOL W}}==
Constructs a sieve of Brazilian numbers from the definition.
<langsyntaxhighlight lang="algolw">begin % find some Brazilian numbers - numbers N whose representation in some %
% base B ( 1 < B < N-1 ) has all the same digits %
% set b( 1 :: n ) to a sieve of Brazilian numbers where b( i ) is true %
Line 312 ⟶ 309:
end_1000000:
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 331 ⟶ 328:
1000000th Brazilian number: 1084566
</pre>
 
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">on isBrazilian(n)
repeat with b from 2 to n - 2
set d to n mod b
Line 394 ⟶ 390:
end runTask
 
return runTask()</langsyntaxhighlight>
 
{{output}}
 
<langsyntaxhighlight lang="applescript">"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"</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">brazilian?: function [n][
if n < 7 -> return false
if zero? and n 1 -> return true
Line 432 ⟶ 427:
printFirstByRule [i: i+1] ""
printFirstByRule [i: i+2] "odd "
printFirstByRule [i: i+2, while [not? prime? i] -> i: i+2] "prime "</langsyntaxhighlight>
 
{{out}}
Line 446 ⟶ 441:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f BRAZILIAN_NUMBERS.AWK
# converted from C
Line 510 ⟶ 505:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 517 ⟶ 512:
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|BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|Modula-2}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
1000 REM Brazilian numbers
1010 DECLARE EXTERNAL FUNCTION IsBrazilian
1020 DECLARE EXTERNAL FUNCTION SameDigits
1030 DECLARE EXTERNAL FUNCTION IsPrime
1040 PRINT "First 20 Brazilian numbers:"
1050 LET C = 0
1060 LET N = 7
1070 DO WHILE C < 20
1080 IF IsBrazilian(N) <> 0 THEN
1090 PRINT N;
1100 LET C = C + 1
1110 END IF
1120 LET N = N + 1
1130 LOOP
1140 PRINT
1150 PRINT
1160 PRINT "First 20 odd Brazilian numbers:"
1170 LET C = 0
1180 LET N = 7
1190 DO WHILE C < 20
1200 IF IsBrazilian(N) <> 0 THEN
1210 PRINT N;
1220 LET C = C + 1
1230 END IF
1240 LET N = N + 2
1250 LOOP
1260 PRINT
1270 PRINT
1280 PRINT "First 20 prime Brazilian numbers:"
1290 LET C = 0
1300 LET N = 7
1310 DO WHILE C < 20
1320 IF IsBrazilian(N) <> 0 THEN
1330 PRINT N;
1340 LET C = C + 1
1350 END IF
1360 DO
1370 LET N = N + 2
1380 LOOP WHILE IsPrime(N) = 0
1390 LOOP
1400 PRINT
1410 END
1420 REM
1430 EXTERNAL FUNCTION IsBrazilian(N)
1440 REM Result: 1 if N is Brazilian, 0 otherwise
1450 IF N < 7 THEN
1460 LET IsBrazilian = 0
1470 ELSEIF (MOD(N, 2) = 0) AND (N >= 8) THEN
1480 LET IsBrazilian = 1
1490 ELSE
1500 FOR B = 2 TO N - 2
1510 IF SameDigits(N, B) <> 0 THEN
1520 LET IsBrazilian = 1
1530 EXIT FUNCTION
1540 END IF
1550 NEXT B
1560 LET IsBrazilian = 0
1570 END IF
1580 END FUNCTION
1590 REM
1600 EXTERNAL FUNCTION SameDigits(N, B)
1610 REM Result: 1 if N has same digits in the base B, 0 otherwise
1620 LET NL = N ! Local N
1630 LET F = MOD(NL, B)
1640 LET NL = INT(NL / B)
1650 DO WHILE NL > 0
1660 IF MOD(NL, B) <> F THEN
1670 LET SameDigits = 0
1680 EXIT FUNCTION
1690 END IF
1700 LET NL = INT(NL / B)
1710 LOOP
1720 LET SameDigits = 1
1730 END FUNCTION
1740 REM
1750 EXTERNAL FUNCTION IsPrime(N)
1760 REM Result: non-zero if N is prime, 0 otherwise
1770 IF N < 2 THEN
1780 LET IsPrime = 0
1790 ELSEIF MOD(N, 2) = 0 THEN
1800 REM IsPrime = (N = 2)
1810 IF N = 2 THEN
1820 LET IsPrime = 1
1830 ELSE
1840 LET IsPrime = 0
1850 END IF
1860 ELSEIF MOD(N, 3) = 0 THEN
1870 REM IsPrime = (N = 3)
1880 IF N = 3 THEN
1890 LET IsPrime = 1
1900 ELSE
1910 LET IsPrime = 0
1920 END IF
1930 ELSE
1940 LET D = 5
1950 DO WHILE D * D <= N
1960 IF MOD(N, D) = 0 THEN
1970 LET IsPrime = 0
1980 EXIT FUNCTION
1990 ELSE
2000 LET D = D + 2
2010 IF MOD(N, D) = 0 THEN
2020 LET IsPrime = 0
2030 EXIT FUNCTION
2040 ELSE
2050 LET D = D + 4
2060 END IF
2070 END IF
2080 LOOP
2090 LET IsPrime = 1
2100 END IF
2110 END FUNCTION
</syntaxhighlight>
{{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}}===
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="freebasic">Function sameDigits(Byval n As Integer, Byval b As Integer) As Boolean
Dim f As Integer = n Mod b : n \= b
While n > 0
If n Mod b <> f Then Return False Else n \= b
Wend
Return True
End Function
 
Function isBrazilian(Byval n As Integer) As Boolean
If n < 7 Then Return False
If n Mod 2 = 0 Then Return True
For b As Integer = 2 To n - 2
If sameDigits(n, b) Then Return True
Next b
Return False
End Function
 
Function isPrime(Byval n As Integer) As Boolean
If n < 2 Then Return False
If n Mod 2 = 0 Then Return n = 2
If n Mod 3 = 0 Then Return n = 3
Dim d As Integer = 5
While d * d <= n
If n Mod d = 0 Then Return False Else d += 2
If n Mod d = 0 Then Return False Else d += 4
Wend
Return True
End Function
 
Dim kind(2) As String ={"", "odd", "prime"}
For i As Integer = 0 To 2
Print Using "First 20 & Brazilian numbers: "; kind(i)
Dim Limit As Integer = 20, n As Integer = 7
Do
If isBrazilian(n) Then Print Using "& "; n; : Limit -= 1
Select Case kind(i)
Case "" : n += 1
Case "odd" : n += 2
Case "prime" : Do : n += 2 : Loop Until isPrime(n)
End Select
Loop While Limit > 0
Next i
Sleep</syntaxhighlight>
 
==={{header|Nascom BASIC}}===
{{trans|Modula-2}}
{{works with|Nascom ROM BASIC|4.7}}
This version shows only the first 12 prime Brazilian numbers because of slow work.
<syntaxhighlight lang="basic">
10 REM Brazilian numbers
20 PRINT "First 20 Brazilian numbers:"
30 C=0:N=7
40 IF C>=20 THEN 90
50 GOSUB 320
60 IF BR THEN PRINT N;:C=C+1
70 N=N+1
80 GOTO 40
90 PRINT:PRINT
100 PRINT "First 20 odd Brazilian numbers:"
110 C=0:N=7
120 IF C>=20 THEN 170
130 GOSUB 320
140 IF BR THEN PRINT N;:C=C+1
150 N=N+2
160 GOTO 120
170 PRINT:PRINT
180 PRINT "First 12 prime Brazilian numbers:"
190 C=0:N=7
200 IF C>=12 THEN 270
210 GOSUB 320
220 IF BR THEN PRINT N;:C=C+1
230 N=N+2
240 GOSUB 530
250 IF NOT PRM THEN 230
260 GOTO 200
270 PRINT
280 END
290 REM ** Is Brazilian?
300 REM Result: BR=-1 if N is Brazilian
310 REM 0 otherwise
320 IF N<7 THEN BR=0:RETURN
330 IF INT(N/2)*2=N AND N>=8 THEN BR=-1:RETURN
340 FOR B=2 TO N-2
350 GOSUB 430
360 IF SMD THEN BR=-1:RETURN
370 NEXT B
380 BR=0
390 RETURN
400 REM ** Same digits?
410 REM Result: SMD=-1 if N has same digits in B,
420 REM 0 otherwise
430 NL=N:REM "Local" N
440 NDB=INT(NL/B)
450 F=NL-NDB*B
460 NL=NDB:NDB=INT(NL/B)
470 IF NL<=0 THEN SMD=-1:RETURN
480 IF NL-NDB*B<>F THEN SMD=0:RETURN
490 NL=INT(NL/B):NDB=INT(NL/B)
500 GOTO 470
510 REM ** Is prime?
520 REM Result: PRM=-1 if N prime, 0 otherwise
530 IF N<2 THEN PRM=0:RETURN
540 IF INT(N/2)*2=N THEN PRM=N=2:RETURN
550 IF INT(N/3)*3=N THEN PRM=N=3:RETURN
560 D=5
570 IF D*D>N THEN PRM=-1:RETURN
580 IF INT(N/D)*D=N THEN PRM=0:RETURN
590 D=D+2
600 IF INT(N/D)*D=N THEN PRM=0:RETURN
610 D=D+4
620 GOTO 570
</syntaxhighlight>
{{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 12 prime Brazilian numbers:
7 13 31 43 73 127 157 211 241 307 421 463
</pre>
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function sameDigits(ByVal n As Integer, ByVal b As Integer) As Boolean
Dim f As Integer = n Mod b : n \= b : While n > 0
If n Mod b <> f Then Return False Else n \= b
End While : Return True
End Function
 
Function isBrazilian(ByVal n As Integer) As Boolean
If n < 7 Then Return False
If n Mod 2 = 0 Then Return True
For b As Integer = 2 To n - 2
If sameDigits(n, b) Then Return True
Next : Return False
End Function
 
Function isPrime(ByVal n As Integer) As Boolean
If n < 2 Then Return False
If n Mod 2 = 0 Then Return n = 2
If n Mod 3 = 0 Then Return n = 3
Dim d As Integer = 5
While d * d <= n
If n Mod d = 0 Then Return False Else d += 2
If n Mod d = 0 Then Return False Else d += 4
End While : Return True
End Function
 
Sub Main(args As String())
For Each kind As String In {" ", " odd ", " prime "}
Console.WriteLine("First 20{0}Brazilian numbers:", kind)
Dim Limit As Integer = 20, n As Integer = 7
Do
If isBrazilian(n) Then Console.Write("{0} ", n) : Limit -= 1
Select Case kind
Case " " : n += 1
Case " odd " : n += 2
Case " prime " : Do : n += 2 : Loop Until isPrime(n)
End Select
Loop While Limit > 0
Console.Write(vbLf & vbLf)
Next
End Sub
 
End Module</syntaxhighlight>
{{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>
 
====Speedier Version====
Based on the C# speedier version, performance is just as good, one billion Brazilian numbers counted in 4 1/2 seconds (on a core i7).
<syntaxhighlight lang="vbnet">Imports System
 
Module Module1
' flags:
Const _
PrMk As Integer = 0, ' a number that is prime
SqMk As Integer = 1, ' a number that is the square of a prime number
UpMk As Integer = 2, ' a number that can be factored (aka un-prime)
BrMk As Integer = -2, ' a prime number that is also a Brazilian number
Excp As Integer = 121 ' exception square - the only square prime that is a Brazilian
 
Dim pow As Integer = 9,
max As Integer ' maximum sieve array length
' An upper limit of the required array length can be calculated Like this:
' power of 10 fraction limit actual result
' 1 2 / 1 * 10 = 20 20
' 2 4 / 3 * 100 = 133 132
' 3 6 / 5 * 1000 = 1200 1191
' 4 8 / 7 * 10000 = 11428 11364
' 5 10/ 9 * 100000 = 111111 110468
' 6 12/11 * 1000000 = 1090909 1084566
' 7 14/13 * 10000000 = 10769230 10708453
' 8 16/15 * 100000000 = 106666666 106091516
' 9 18/17 * 1000000000 = 1058823529 1053421821
' powers above 9 are impractical because of the maximum array length in VB.NET,
' which is around the UInt32.MaxValue, Or 4294967295
 
Dim PS As SByte() ' the prime/Brazilian number sieve
' once the sieve is populated, primes are <= 0, non-primes are > 0,
' Brazilian numbers are (< 0) or (> 1)
' 121 is a special case, in the sieve it is marked with the BrMk (-2)
 
' typical sieve of Eratosthenes algorithm
Sub PrimeSieve(ByVal top As Integer)
PS = New SByte(top) {} : Dim i, ii, j As Integer
i = 2 : j = 4 : PS(j) = SqMk : While j < top - 2 : j += 2 : PS(j) = UpMk : End While
i = 3 : j = 9 : PS(j) = SqMk : While j < top - 6 : j += 6 : PS(j) = UpMk : End While
i = 5 : ii = 25 : While ii < top
If PS(i) = PrMk Then
j = (top - i) / i : If (j And 1) = 0 Then j -= 1
Do : If PS(j) = PrMk Then PS(i * j) = UpMk
j -= 2 : Loop While j > i : PS(ii) = SqMk
End If
Do : i += 2 : Loop While PS(i) <> PrMk : ii = i * i
End While
End Sub
 
' consults the sieve and returns whether a number is Brazilian
Function IsBr(ByVal number As Integer) As Boolean
Return Math.Abs(PS(number)) > SqMk
End Function
 
' shows the first few Brazilian numbers of several kinds
Sub FirstFew(ByVal kind As String, ByVal amt As Integer)
Console.WriteLine(vbLf & "The first {0} {1}Brazilian Numbers are:", amt, kind)
Dim i As Integer = 7 : While amt > 0
If IsBr(i) Then amt -= 1 : Console.Write("{0} ", i)
Select Case kind : Case "odd " : i += 2
Case "prime " : Do : i += 2 : Loop While PS(i) <> BrMk OrElse i = Excp
Case Else : i += 1 : End Select : End While : Console.WriteLine()
End Sub
 
' expands a 111_X number into an integer
Function Expand(ByVal NumberOfOnes As Integer, ByVal Base As Integer) As Integer
Dim res As Integer = 1
While NumberOfOnes > 1 AndAlso res < Integer.MaxValue \ Base
res = res * Base + 1 : NumberOfOnes -= 1 : End While
If res > max OrElse res < 0 Then res = 0
Return res
End Function
 
' returns an elapsed time string
Function TS(ByVal fmt As String, ByRef st As DateTime, ByVal Optional reset As Boolean = False) As String
Dim n As DateTime = DateTime.Now,
res As String = String.Format(fmt, (n - st).TotalMilliseconds)
If reset Then st = n
Return res
End Function
 
Sub Main(args As String())
Dim p2 As Integer = pow << 1, primes(6) As Integer, n As Integer,
st As DateTime = DateTime.Now, st0 As DateTime = st,
p10 As Integer = CInt(Math.Pow(10, pow)), p As Integer = 10, cnt As Integer = 0
max = CInt(((CLng((p10)) * p2) / (p2 - 1))) : PrimeSieve(max)
Console.WriteLine(TS("Sieving took {0} ms", st, True))
' make short list of primes before Brazilians are added
n = 3 : For i As Integer = 0 To primes.Length - 1
primes(i) = n : Do : n += 2 : Loop While PS(n) <> 0 : Next
Console.WriteLine(vbLf & "Checking first few prime numbers of sequential ones:" &
vbLf & "ones checked found")
' now check the '111_X' style numbers. many are factorable, but some are prime,
' then re-mark the primes found in the sieve as Brazilian.
' curiously, only the numbers with a prime number of ones will turn out, so
' restricting the search to those saves time. no need to wast time on even numbers of ones,
' or 9 ones, 15 ones, etc...
For Each i As Integer In primes
Console.Write("{0,4}", i) : cnt = 0 : n = 2 : Do
If (n - 1) Mod i <> 0 Then
Dim br As Long = Expand(i, n)
If br > 0 Then
If PS(br) < UpMk Then PS(br) = BrMk : cnt += 1
Else
Console.WriteLine("{0,8}{1,6}", n, cnt) : Exit Do
End If
End If : n += 1 : Loop While True
Next
Console.WriteLine(TS("Adding Brazilian primes to the sieve took {0} ms", st, True))
For Each s As String In ",odd ,prime ".Split(",") : FirstFew(s, 20) : Next
Console.WriteLine(TS(vbLf & "Required output took {0} ms", st, True))
Console.WriteLine(vbLf & "Decade count of Brazilian numbers:")
n = 6 : cnt = 0 : Do : While cnt < p : n += 1 : If IsBr(n) Then cnt += 1
End While
Console.WriteLine("{0,15:n0}th is {1,-15:n0} {2}", cnt, n, TS("time: {0} ms", st))
If p < p10 Then p *= 10 Else Exit Do
Loop While (True) : PS = New SByte(-1) {}
Console.WriteLine(vbLf & "Total elapsed was {0} ms", (DateTime.Now - st0).TotalMilliseconds)
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
 
End Module
</syntaxhighlight>
{{out}}
<pre>Sieving took 2967.834 ms
 
Checking first few prime numbers of sequential ones:
ones checked found
3 32540 3923
5 182 44
7 32 9
11 8 1
13 8 3
17 4 1
19 4 1
Adding Brazilian primes to the sieve took 8.6242 ms
 
The first 20 Brazilian Numbers are:
7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33
 
The first 20 odd Brazilian Numbers are:
7 13 15 21 27 31 33 35 39 43 45 51 55 57 63 65 69 73 75 77
 
The first 20 prime Brazilian Numbers are:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
 
Required output took 2.8256 ms
 
Decade count of Brazilian numbers:
10th is 20 time: 0.0625 ms
100th is 132 time: 0.1156 ms
1,000th is 1,191 time: 0.1499 ms
10,000th is 11,364 time: 0.1986 ms
100,000th is 110,468 time: 0.4081 ms
1,000,000th is 1,084,566 time: 1.9035 ms
10,000,000th is 10,708,453 time: 15.9129 ms
100,000,000th is 106,091,516 time: 149.8814 ms
1,000,000,000th is 1,053,421,821 time: 1412.3526 ms
 
Total elapsed was 4391.7287 ms
</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.
 
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
typedef char bool;
Line 593 ⟶ 1,064:
printf("The 100,000th Brazilian number: %d\n", n - 1);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 608 ⟶ 1,079:
The 100,000th Brazilian number: 110468
</pre>
=={{header|C sharp|C#}}==
 
=={{header|C#|CSharp}}==
{{trans|Go}}
<langsyntaxhighlight lang="csharp">using System;
class Program {
Line 663 ⟶ 1,133:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 680 ⟶ 1,150:
===Speedier Version===
Based on the Pascal version, with some shortcuts. Can calculate to one billion in under 4 1/2 seconds (on a core i7). This is faster than the Pascal version because the sieve is an array of '''SByte''' (8 bits) and not a '''NativeUInt''' (32 bits). Also this code does not preserve the base of each Brazilain number in the array, so the Pascal version is more flexible if desiring to quickly verify a quantity of Brazilian numbers.
<langsyntaxhighlight lang="csharp">using System;
 
class Program
Line 778 ⟶ 1,248:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}</langsyntaxhighlight>
{{out}}
<pre>Sieving took 3009.2927 ms
Line 816 ⟶ 1,286:
 
Total elapsed was 4469.1985 ms</pre> P.S. The best speed on Tio.run is under 5 seconds for the 100 millionth count ('''pow''' = 8). If you are very persistent, the 1 billionth count ('''pow''' = 9) can be made to work on Tio.run, it usually overruns the 60 second timeout limit, and cannot finish completely - the sieving by itself takes over 32 seconds (best case), which usually doesn't leave enough time for all the counting.
 
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
bool sameDigits(int n, int b) {
Line 894 ⟶ 1,363:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazillian numbers:
Line 906 ⟶ 1,375:
First 20 prime Brazillian numbers:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801</pre>
 
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.stdio;
 
bool sameDigits(int n, int b) {
Line 977 ⟶ 1,445:
if (kind == "") writefln("The %sth Brazillian number is: %s\n", BigLim + 1, n);
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazillion numbers:
Line 989 ⟶ 1,457:
First 20 prime Brazillion numbers:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801</pre>
 
=={{header|Delphi}}==
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Brazilian_numbers;
 
Line 1,169 ⟶ 1,636:
end.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,184 ⟶ 1,651:
The 100,000th Brazilian number: 110468
</pre>
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang="easylang">
func sameDigits n b .
f = n mod b
repeat
n = n div b
until n = 0
if n mod b <> f
return 0
.
.
return 1
.
func isBrazilian7 n .
# n >= 7
if n mod 2 = 0
return 1
.
for b = 2 to n - 2
if sameDigits n b = 1
return 1
.
.
return 0
.
func prime n .
if n mod 2 = 0 and n > 2
return 0
.
i = 3
sq = sqrt n
while i <= sq
if n mod i = 0
return 0
.
i += 2
.
return 1
.
for kind$ in [ "" "odd" "prime" ]
print "First 20 " & kind$ & " Brazilian numbers:"
n = 7
cnt = 1
while cnt <= 20
if isBrazilian7 n = 1
write n & " "
cnt += 1
.
if kind$ = ""
n += 1
elif kind$ = "odd"
n += 2
else
repeat
n += 2
until prime n = 1
.
.
.
print ""
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
===The functions===
<langsyntaxhighlight lang="fsharp">
// Generate Brazilian sequence. Nigel Galloway: August 13th., 2019
let isBraz α=let mutable n,i,g=α,α+1,1 in (fun β->(while (i*g)<β do if g<α-1 then g<-g+1 else (n<-n*α; i<-n+i; g<-1)); β=i*g)
Line 1,194 ⟶ 1,724:
yield! fN (n+1) ((isBraz (n-1))::g)}
fN 4 [isBraz 2]
</syntaxhighlight>
</lang>
===The Tasks===
;the first 20 Brazilian numbers
<langsyntaxhighlight lang="fsharp">
Brazilian() |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,205 ⟶ 1,735:
</pre>
;the first 20 odd Brazilian numbers
<langsyntaxhighlight lang="fsharp">
Brazilian() |> Seq.filter(fun n->n%2=1) |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,214 ⟶ 1,744:
;the first 20 prime Brazilian numbers
Using [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
Brazilian() |> Seq.filter isPrime |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,222 ⟶ 1,752:
</pre>
;finally that which the crowd really want to know: What is the 100,000<sup>th</sup> Brazilian number?
<langsyntaxhighlight lang="fsharp">
printfn "%d" (Seq.item 99999 Brazilian)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,233 ⟶ 1,763:
=={{header|Factor}}==
{{works with|Factor|0.99 development release 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: combinators grouping io kernel lists lists.lazy math
math.parser math.primes.lists math.ranges namespaces prettyprint
prettyprint.config sequences ;
Line 1,254 ⟶ 1,784:
1 [ 2 + ] lfrom-by "First 20 odd Brazilian numbers:"
lprimes "First 20 prime Brazilian numbers:"
[ print .20-brazilians nl ] 2tri@</langsyntaxhighlight>
{{out}}
<pre>
Line 1,266 ⟶ 1,796:
{ 7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801 }
</pre>
 
=={{header|Fōrmulæ}}==
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
In '''[https://formulae.org/?example=Brazilian_numbers this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: prime? ( n -- flag )
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
Line 1,344 ⟶ 1,865:
0 20 print_brazilian
 
bye</langsyntaxhighlight>
 
{{out}}
Line 1,357 ⟶ 1,878:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
</pre>
 
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
<lang Fortran>
!Constructs a sieve of Brazilian numbers from the definition.
!From the Algol W algorithm, somewhat "Fortranized"
Line 1,533 ⟶ 2,053:
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,555 ⟶ 2,075:
</pre>
 
=={{header|FreeBASICFōrmulæ}}==
{{trans|Visual Basic .NET}}
<lang freebasic>Function sameDigits(Byval n As Integer, Byval b As Integer) As Boolean
Dim f As Integer = n Mod b : n \= b
While n > 0
If n Mod b <> f Then Return False Else n \= b
Wend
Return True
End Function
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Brazilian_numbers}}
Function isBrazilian(Byval n As Integer) As Boolean
If n < 7 Then Return False
If n Mod 2 = 0 Then Return True
For b As Integer = 2 To n - 2
If sameDigits(n, b) Then Return True
Next b
Return False
End Function
 
'''Solution'''
Function isPrime(Byval n As Integer) As Boolean
If n < 2 Then Return False
If n Mod 2 = 0 Then Return n = 2
If n Mod 3 = 0 Then Return n = 3
Dim d As Integer = 5
While d * d <= n
If n Mod d = 0 Then Return False Else d += 2
If n Mod d = 0 Then Return False Else d += 4
Wend
Return True
End Function
 
Function to determine if the given number is Brazilian:
Dim kind(2) As String ={"", "odd", "prime"}
 
For i As Integer = 0 To 2
[[File:Fōrmulæ Print Using "First 20 &- Brazilian numbers: "; kind(i)01.png]]
 
Dim Limit As Integer = 20, n As Integer = 7
The next function calculates the first Brazilian numbers that agree the given condition. Notice that the condition is provided as a lambda expression:
Do
 
If isBrazilian(n) Then Print Using "& "; n; : Limit -= 1
[[File:Fōrmulæ - Brazilian numbers 02.png]]
Select Case kind(i)
 
Case "" : n += 1
'''Test case 1.''' First 20 Brazilian numbers:
Case "odd" : n += 2
 
Case "prime" : Do : n += 2 : Loop Until isPrime(n)
[[File:Fōrmulæ - Brazilian numbers 03.png]]
End Select
 
Loop While Limit > 0
[[File:Fōrmulæ - Brazilian numbers 04.png]]
Next i
 
Sleep</lang>
'''Test case 2.''' First 20 odd Brazilian numbers:
 
[[File:Fōrmulæ - Brazilian numbers 05.png]]
 
[[File:Fōrmulæ - Brazilian numbers 06.png]]
 
'''Test case 3.''' First 20 prime Brazilian numbers:
 
[[File:Fōrmulæ - Brazilian numbers 07.png]]
 
[[File:Fōrmulæ - Brazilian numbers 08.png]]
 
=={{header|Go}}==
===Version 1===
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,697 ⟶ 2,202:
}
fmt.Println("The 100,000th Brazilian number:", n-1)
}</langsyntaxhighlight>
 
{{out}}
Line 1,719 ⟶ 2,224:
 
Running a bit quicker than the .NET versions though not due to any further improvements on my part.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,911 ⟶ 2,416:
}
fmt.Printf("\nTotal elapsed was %.4f ms\n", toMs(time.Since(st0)))
}</langsyntaxhighlight>
 
{{out}}
Line 1,953 ⟶ 2,458:
Total elapsed was 3249.0197 ms
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">import org.codehaus.groovy.GroovyBugError
 
class Brazilian {
Line 2,055 ⟶ 2,559:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 2,067 ⟶ 2,571:
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|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes (primes)
 
isBrazil :: Int -> Bool
Line 2,095 ⟶ 2,598:
, "\n"
])
[([], [1 ..]), ("odd", [1,3 ..]), ("prime", primes)]</langsyntaxhighlight>
{{Out}}
<pre>First 20 Brazilians:
Line 2,105 ⟶ 2,608:
First 20 prime Brazilians:
[7,13,31,43,73,127,157,211,241,307,421,463,601,757,1093,1123,1483,1723,2551,2801]</pre>
 
 
=={{header|Isabelle}}==
{{works with|Isabelle|2020}}
Line 2,112 ⟶ 2,613:
Not the most beautiful proofs and the theorem about "R*S >= 8, with S+1 > R, are Brazilian" is missing.
 
<langsyntaxhighlight Isabellelang="isabelle">theory Brazilian
imports Main
begin
Line 2,339 ⟶ 2,840:
(*TODO: the first 20 prime Brazilian numbers*)
 
end</langsyntaxhighlight>
 
=={{header|J}}==
The brazilian verb checks if 1 is the tally of one of the sets of values in the possible base representations.
<syntaxhighlight lang="text">
Doc=: conjunction def 'u :: (n"_)'
 
Line 2,369 ⟶ 2,869:
20 {. brazilian Filter p: 2 + i. 500
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.math.BigInteger;
import java.util.List;
 
Line 2,469 ⟶ 2,968:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 2,481 ⟶ 2,980:
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|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
 
<syntaxhighlight lang="jq"># Output: a stream of digits, least significant digit first
def to_base($base):
def butlast(s):
label $out
| foreach (s,null) as $x ({};
if $x == null then break $out else .emit = .prev | .prev = $x end;
select(.emit).emit);
 
if . == 0 then 0
else butlast(recurse( if . == 0 then empty else ./$base | floor end ) % $base)
end ;
 
 
def sameDigits($n; $b):
($n % $b) as $f
| all( ($n | to_base($b)); . == $f) ;
def isBrazilian:
. as $n
| if . < 7 then false
elif (.%2 == 0 and . >= 8) then true
else any(range(2; $n-1); sameDigits($n; .) )
end;
 
def brazilian_numbers($m; $n; $step):
range($m; $n; $step)
| select(isBrazilian);
 
def task($n):
"First \($n) Brazilian numbers:",
limit($n; brazilian_numbers(7; infinite; 1)),
"First \($n) odd Brazilian numbers:",
limit($n; brazilian_numbers(7; infinite; 2)),
"First \($n) prime Brazilian numbers:",
limit($n; brazilian_numbers(7; infinite; 2) | select(is_prime)) ;
 
task(20)
 
</syntaxhighlight>
{{out}}
As elsewhere, e.g. [[#Python]].
=={{header|Julia}}==
{{trans|Go}}
<langsyntaxhighlight lang="julia">using Primes, Lazy
 
function samedigits(n, b)
Line 2,507 ⟶ 3,052:
 
println("The first 20 prime Brazilian numbers are: ", take(20, primebrazilians))
</langsyntaxhighlight>{{out}}
<pre>
The first 20 Brazilian numbers are: (7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33)
Line 2,515 ⟶ 3,060:
 
There has been some discussion of larger numbers in the sequence. See below:
<langsyntaxhighlight lang="julia">function braziliandensities(N, interval)
count, intervalcount, icount = 0, 0, 0
intervalcounts = Int[]
Line 2,539 ⟶ 3,084:
braziliandensities(100000, 1000)
plot(1:1000:1000000, braziliandensities(1000000, 1000))
</langsyntaxhighlight>{{out}}
<pre>
The 10000 th brazilian is 11364.
Line 2,546 ⟶ 3,091:
</pre>
 
[[File:Brazilian densities.png|thumb|center]]
[http://alahonua.com/temp/newplot.png link plot png]
 
=={{header|Kotlin}}==
{{trans|C#}}
<langsyntaxhighlight lang="scala">fun sameDigits(n: Int, b: Int): Boolean {
var n2 = n
val f = n % b
Line 2,621 ⟶ 3,166:
if (kind == "") println("The %,dth Brazilian number is: %,d".format(bigLim + 1, n))
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 2,632 ⟶ 3,177:
First 20 primeBrazilian numbers:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1,093 1,123 1,483 1,723 2,551 2,801 </pre>
 
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function sameDigits(n,b)
local f = n % b
n = math.floor(n / b)
Line 2,719 ⟶ 3,263:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazillion numbers:
Line 2,729 ⟶ 3,273:
First 20 prime Brazillion numbers:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight wolframlang="mathematica">brazilianQ[n_Integer /; n>6 ] := AnyTrue[
Range[2, n-2],
MatchQ[IntegerDigits[n, #], {x_ ...}] &
Line 2,738 ⟶ 3,281:
Select[Range[100], brazilianQ@# && OddQ@# &, 20]
Select[Range[10000], brazilianQ@# && PrimeQ@# &, 20]
</syntaxhighlight>
</lang>
{{out}}
<pre>{7, 8, 10, 12, 13, 14, 15, 16, 18, 20, 21, 22, 24, 26, 27, 28, 30, 31, 32, 33}
{7, 13, 15, 21, 27, 31, 33, 35, 39, 43, 45, 51, 55, 57, 63, 65, 69, 73, 75, 77}
{7, 13, 31, 43, 73, 127, 157, 211, 241, 307, 421, 463, 601, 757, 1093, 1123, 1483, 1723, 2551, 2801}</pre>
 
=={{header|MATLAB}}==
{{trans|Mathematica_/_Wolfram_Language}}
<syntaxhighlight lang="MATLAB">
clear all;close all;clc;
 
% Find the first 20 Brazilian numbers in the range 7 to 100.
brazilian_numbers = [];
for num = 7:100
if brazilianQ(num)
brazilian_numbers = [brazilian_numbers, num];
if length(brazilian_numbers) == 20
break;
end
end
end
% For Brazilian numbers
fprintf('% 8d', brazilian_numbers);
fprintf("\n");
 
 
% Find the first 20 odd Brazilian numbers in the range 7 to 100.
odd_brazilian_numbers = [];
for num = 7:100
if brazilianQ(num) && mod(num, 2) ~= 0
odd_brazilian_numbers = [odd_brazilian_numbers, num];
if length(odd_brazilian_numbers) == 20
break;
end
end
end
% For odd Brazilian numbers
fprintf('% 8d', odd_brazilian_numbers);
fprintf("\n");
 
 
% Find the first 20 Brazilian prime numbers in the range 7 to 10000.
brazilian_prime_numbers = [];
for num = 7:10000
if brazilianQ(num) && isprime(num)
brazilian_prime_numbers = [brazilian_prime_numbers, num];
if length(brazilian_prime_numbers) == 20
break;
end
end
end
% For Brazilian prime numbers
fprintf('% 8d', brazilian_prime_numbers);
fprintf("\n");
 
 
 
function isBrazilian = brazilianQ(n)
% Function to check if a number is a Brazilian number.
if n <= 6
error('Input must be greater than 6.');
end
isBrazilian = false;
for b = 2:(n-2)
base_b_digits = custom_dec2base(n, b) - '0'; % Convert number to base b and then to digits
if all(base_b_digits == base_b_digits(1))
isBrazilian = true;
break;
end
end
end
 
function digits = custom_dec2base(num, base)
% Custom function to convert number to any base representation
if base < 2 || base > num
error('Base must be at least 2 and less than the number itself.');
end
digits = [];
while num > 0
remainder = mod(num, base);
digits = [remainder, digits];
num = floor(num / base);
end
end
</syntaxhighlight>
{{out}}
<pre>
7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33
7 13 15 21 27 31 33 35 39 43 45 51 55 57 63 65 69 73 75 77
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
</pre>
 
 
=={{header|Modula-2}}==
{{trans|C|<code>CARDINAL</code> (unsigned integer) used instead of signed integer.<code>ELSIF</code> used instead of sequential <code>IF</code>s with <code>RETURN</code> s. In the main program, the loop <code>for (i = 0; i < 3; ++i) </code> is replaced by subsequent actions, because the actions vary depending on <code>i</code>.}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<syntaxhighlight lang="modula2">
MODULE BrazilianNumbers;
 
FROM STextIO IMPORT
WriteLn, WriteString;
FROM SWholeIO IMPORT
WriteInt;
 
VAR
C, N: CARDINAL;
 
PROCEDURE SameDigits(N, B: CARDINAL): BOOLEAN;
VAR
F: CARDINAL;
BEGIN
F := N MOD B;
N := N / B;
WHILE N > 0 DO
IF N MOD B <> F THEN
RETURN FALSE;
END;
N := N / B;
END;
RETURN TRUE;
END SameDigits;
 
PROCEDURE IsBrazilian(N: CARDINAL): BOOLEAN;
VAR
B: CARDINAL;
BEGIN
IF N < 7 THEN
RETURN FALSE
ELSIF (N MOD 2 = 0) AND (N >= 8) THEN
RETURN TRUE
ELSE
FOR B := 2 TO N - 2 DO
IF SameDigits(N, B) THEN
RETURN TRUE
END
END;
RETURN FALSE;
END
END IsBrazilian;
 
PROCEDURE IsPrime(N: CARDINAL): BOOLEAN;
VAR
D: CARDINAL;
BEGIN
IF N < 2 THEN
RETURN FALSE;
ELSIF N MOD 2 = 0 THEN
RETURN N = 2;
ELSIF N MOD 3 = 0 THEN
RETURN N = 3;
ELSE
D := 5;
WHILE D * D <= N DO
IF N MOD D = 0 THEN
RETURN FALSE
ELSE
D := D + 2;
IF N MOD D = 0 THEN
RETURN FALSE
ELSE
D := D + 4
END
END
END;
RETURN TRUE;
END
END IsPrime;
 
BEGIN
WriteString("First 20 Brazilian numbers:");
WriteLn;
C := 0; N := 7;
WHILE C < 20 DO
IF IsBrazilian(N) THEN
WriteInt(N, 1);
WriteString(" ");
C := C + 1;
END;
N := N + 1;
END;
WriteLn; WriteLn;
WriteString("First 20 odd Brazilian numbers:");
WriteLn;
C := 0; N := 7;
WHILE C < 20 DO
IF IsBrazilian(N) THEN
WriteInt(N, 1);
WriteString(" ");
C := C + 1;
END;
N := N + 2;
END;
WriteLn; WriteLn;
WriteString("First 20 prime Brazilian numbers:");
WriteLn;
C := 0; N := 7;
WHILE C < 20 DO
IF IsBrazilian(N) THEN
WriteInt(N, 1);
WriteString(" ");
C := C + 1;
END;
REPEAT
N := N + 2
UNTIL IsPrime(N);
END;
WriteLn;
END BrazilianNumbers.
</syntaxhighlight>
{{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|Nim}}==
<langsyntaxhighlight Nimlang="nim">proc isPrime(n: Positive): bool =
## Check if a number is prime.
if n mod 2 == 0:
Line 2,813 ⟶ 3,572:
inc n, 2
while not n.isPrime():
inc n, 2</langsyntaxhighlight>
 
{{out}}
Line 2,832 ⟶ 3,591:
extreme reduced runtime time for space.<BR>
At the end only primes and square of primes need to be tested, all others are Brazilian.
<langsyntaxhighlight lang="pascal">program brazilianNumbers;
 
{$IFDEF FPC}
Line 3,123 ⟶ 3,882:
 
setlength(isPrime, 0);
end.</langsyntaxhighlight>
{{out}}
<pre>first 20 brazilian numbers
Line 3,212 ⟶ 3,971:
sys 0m0,157s
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
 
use ntheory qw<is_prime>;
use constant Inf => 1e10;
 
sub is_Brazilian {
Line 3,242 ⟶ 3,999:
 
my $upto = 20;
use constant Inf => 1e10;
 
my $b =print "First $upto Brazilian numbers:\n";
my $n = 0;
$b .=print do { $n < $upto ? (is_Brazilian($_) and ++$n and "$_ ") : last } for 1 .. Inf;
 
$b .=print "\n\nFirst $upto odd Brazilian numbers:\n";
$n = 0;
$b .=print do { $n < $upto ? (!!($_%2) and is_Brazilian($_) and ++$n and "$_ ") : last } for 1 .. Inf;
 
$b .=print "\n\nFirst $upto prime Brazilian numbers:\n";
$n = 0;
$b .=print do { $n < $upto ? (!!is_prime($_) and is_Brazilian($_) and ++$n and "$_ ") : last } for 1 .. Inf;</syntaxhighlight>
 
say $b;</lang>
{{out}}
<pre>First 20 Brazilian numbers:
Line 3,266 ⟶ 4,020:
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|Phix}}==
{{trans|C}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function same_digits(integer n, b)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer f = remainder(n,b)
<span style="color: #008080;">function</span> <span style="color: #000000;">same_digits</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
n = floor(n/b)
<span style="color: #004080;">integer</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
while n>0 do
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
if remainder(n,b)!=f then return false end if
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
n = floor(n/b)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">f</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
return true
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end function
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function is_brazilian(integer n)
if n>=7 then
<span style="color: #008080;">function</span> <span style="color: #000000;">is_brazilian</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
if remainder(n,2)=0 then return true end if
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">7</span> <span style="color: #008080;">then</span>
for b=2 to n-2 do
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if same_digits(n,b) then return true end if
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">same_digits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return false
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant kinds = {" ", " odd ", " prime "}
for i=1 to length(kinds) do
<span style="color: #008080;">constant</span> <span style="color: #000000;">kinds</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" odd "</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" prime "</span><span style="color: #0000FF;">}</span>
printf(1,"First 20%sBrazilian numbers:\n", {kinds[i]})
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">kinds</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer c = 0, n = 7, p = 4
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 20%sBrazilian numbers:\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">kinds</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
while true do
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span>
if is_brazilian(n) then
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
printf(1,"%d ",n)
<span style="color: #008080;">if</span> <span style="color: #000000;">is_brazilian</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
c += 1
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
if c==20 then
<span style="color: #000000;">c</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
printf(1,"\n\n")
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">==</span><span style="color: #000000;">20</span> <span style="color: #008080;">then</span>
exit
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n\n"</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">exit</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
switch i
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
case 1: n += 1
<span style="color: #008080;">switch</span> <span style="color: #000000;">i</span>
case 2: n += 2
<span style="color: #008080;">case</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
case 3: p += 1; n = get_prime(p)
<span style="color: #008080;">case</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span>
end switch
<span style="color: #008080;">case</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
integer n = 7, c = 0
atom t0 = time(), t1 = time()+1
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
while c<100000 do
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
if time()>t1 then
<span style="color: #008080;">while</span> <span style="color: #000000;">c</span><span style="color: #0000FF;"><</span><span style="color: #000000;">100000</span> <span style="color: #008080;">do</span>
printf(1,"checking %d [count:%d]...\r",{n,c})
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
t1 = time()+1
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"checking %d [count:%d]...\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
c += is_brazilian(n)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
n += 1
<span style="color: #000000;">c</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">is_brazilian</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
printf(1,"The %,dth Brazilian number: %d\n", {c,n-1})
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
?elapsed(time()-t0)</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The %,dth Brazilian number: %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
(notNot very fast, takes about 4 times as long as Go(v1), at least on desktop/Phix, however under pwa/p2js it is about the same speed.
<pre>
First 20 Brazilian numbers:
Line 3,337 ⟶ 4,093:
"52.8s"
</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Brazilian numbers'''
 
from itertools import count, islice
Line 3,453 ⟶ 4,208:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 20 Brazilians:
Line 3,463 ⟶ 4,218:
First 20 prime Brazilians:
[7,13,31,43,73,127,157,211,241,307,421,463,601,757,1093,1123,1483,1723,2551,2801]</pre>
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ dup base put
/mod temp put
true swap
[ dup 0 > while
base share /mod
temp share != iff
[ dip not ]
done
again ]
drop
temp release
base release ] is allsame ( n n --> b )
 
[ false swap
dup 3 - times
[ dup i 2 +
allsame iff
[ dip not
conclude ]
done ]
drop ] is brazilian ( n --> b )
 
say "First 20 Brazilian numbers:" cr
[] 0
[ dup brazilian if
[ dup dip join ]
1+
over size 20 = until ]
drop echo
cr
cr
say "First 20 odd Brazilian numbers:" cr
[] 1
[ dup brazilian if
[ dup dip join ]
2 +
over size 20 = until ]
drop echo
cr
cr
say "First 20 prime Brazilian numbers:" cr
[] 1
[ dup isprime not iff
[ 2 + ] again
dup brazilian if
[ dup dip join ]
2 +
over size 20 = until ]
drop echo</syntaxhighlight>
 
{{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|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 3,493 ⟶ 4,314:
(stream->list (stream-take (stream-filter brazilian? (stream-filter odd? (in-naturals))) 20))
(displayln "First 20 prime Brazilian numbers:")
(stream->list (stream-take (stream-filter prime-brazilian? (stream-filter odd? (in-naturals))) 20)))</langsyntaxhighlight>
 
{{out}}
Line 3,502 ⟶ 4,323:
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|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>multi is-Brazilian (Int $n where $n %% 2 && $n > 6) { True }
 
multi is-Brazilian (Int $n) {
Line 3,527 ⟶ 4,347:
put "\nFirst $upto odd Brazilian numbers:\n", (^Inf).hyper.map( * * 2 + 1 ).grep( &is-Brazilian )[^$upto];
 
put "\nFirst $upto prime Brazilian numbers:\n", (^Inf).hyper(:8degree).grep( { .is-prime && .&is-Brazilian } )[^$upto];</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 3,537 ⟶ 4,357:
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|REXX}}==
{{trans|GO}}
<syntaxhighlight lang="text">/*REXX pgm finds: 1st N Brazilian #s; odd Brazilian #s; prime Brazilian #s; ZZZth #.*/
parse arg t.1 t.2 t.3 t.4 . /*obtain optional arguments from the CL*/
if t.4=='' | t.4=="," then t.4= 0 /*special test case of Nth Brazilian #.*/
Line 3,594 ⟶ 4,413:
end /*while*/; return 1 /*it has all the same dig*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
th: parse arg th; return th || word('th st nd rd', 1+(th//10)*(th//100%10\==1)*(th//10<4))</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> , &nbsp; , &nbsp; , &nbsp; 100000 </tt>}}
<pre>
Line 3,609 ⟶ 4,428:
110468
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 3,693 ⟶ 4,511:
txt = left(txt,len(txt)-1)
see txt + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,705 ⟶ 4,523:
7,13,31,43,73,127,157,211,241,1093,2801
done...
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
≪ SWAP OVER IDIV2 ROT → digit base
≪ 1 SF
'''WHILE''' DUP 1 FS? AND '''REPEAT'''
'''IF''' base IDIV2 digit ≠ '''THEN''' 1 CF '''END'''
'''END'''
DROP 1 FS?
≫ ≫ '<span style="color:blue">SAMEDIGITS?</span>' STO
≪ → n
≪ 0
2 n 2 - '''FOR''' b
n b <span style="color:blue">SAMEDIGITS?</span> OR
'''IF''' DUP '''THEN''' n 'b' STO '''END'''
'''NEXT'''
≫ '<span style="color:blue">BRAZILIAN?</span>' STO
≪ → max inc
≪ { } 5
'''DO'''
'''IF''' DUP <span style="color:blue">BRAZILIAN?</span>' '''THEN''' SWAP OVER + SWAP '''END'''
inc EVAL
'''UNTIL''' OVER SIZE max ≥ '''END'''
DROP
≫ '<span style="color:blue">TASK</span>' STO
 
20 ≪ 1 + ≫ <span style="color:blue">TASK</span>
20 ≪ 2 + ≫ <span style="color:blue">TASK</span>
20 ≪ NEXTPRIME ≫ <span style="color:blue">TASK</span>
{{out}}
<pre>
3: {7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33}
2: {7 13 15 21 27 31 33 35 39 43 45 51 55 57 63 65 69 73 75 77}
1: {7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801}
</pre>
 
=={{header|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang="ruby">def sameDigits(n,b)
f = n % b
while (n /= b) > 0 do
Line 3,804 ⟶ 4,658:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 3,818 ⟶ 4,672:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
fn same_digits(x: u64, base: u64) -> bool {
let f = x % base;
Line 3,883 ⟶ 4,737:
println!("\nThe {}th Brazilian number: {}", big_limit, big_result);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,899 ⟶ 4,753:
=={{header|Scala}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">object BrazilianNumbers {
private val PRIME_LIST = List(
2, 3, 5, 7, 9, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
Line 4,006 ⟶ 4,860:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 4,018 ⟶ 4,872:
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>
===functional===
use Scala 3
<syntaxhighlight lang="scala">object Brazilian extends App {
def oddPrimes =
LazyList.from(3, 2).filter(p => (3 to math.sqrt(p).ceil.toInt by 2).forall(p % _ > 0))
val primes = 2 #:: oddPrimes
def sameDigits(num: Int, base: Int): Boolean = {
val first = num % base
@annotation.tailrec
def iter(num: Int): Boolean = {
if (num % base) == first then iter(num / base)
else num == 0
}
iter(num / base)
}
 
def isBrazilian(num: Int): Boolean = {
num match {
case x if x < 7 => false
case x if (x & 1) == 0 => true
case _ => (2 to num - 2).exists(sameDigits(num,_))
}
}
 
val (limit, bigLimit) = (20, 100_000)
val doList = Seq(("brazilian", LazyList.from(7)),
("odd", LazyList.from(7, 2)),
("prime", primes))
for((listStr, stream) <- doList)
println(s"$listStr: " + stream.filter(isBrazilian(_)).take(limit).toList)
println("be a little patient, it will take some time")
val bigElement = LazyList.from(7).filter(isBrazilian(_)).drop(bigLimit - 1).head
println(s"brazilian($bigLimit): $bigElement")
}</syntaxhighlight>
{{out}}
<pre>brazilian: List(7, 8, 10, 12, 13, 14, 15, 16, 18, 20, 21, 22, 24, 26, 27, 28, 30, 31, 32, 33)
odd: List(7, 13, 15, 21, 27, 31, 33, 35, 39, 43, 45, 51, 55, 57, 63, 65, 69, 73, 75, 77)
prime: List(7, 13, 31, 43, 73, 127, 157, 211, 241, 307, 421, 463, 601, 757, 1093, 1123, 1483, 1723, 2551, 2801)
be a little patient, it will take some time
brazilian(100000): 110468
</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_Brazilian_prime(q) {
 
static L = Set()
Line 4,062 ⟶ 4,961:
say "\nFirst #{n} prime Brazilian numbers"
say (^Inf -> lazy.grep(is_Brazilian).grep{.is_prime}.first(n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,076 ⟶ 4,975:
 
Extra:
<langsyntaxhighlight lang="ruby">for n in (1..6) {
say ("#{10**n->commify}th Brazilian number = ", is_Brazilian.nth(10**n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,089 ⟶ 4,988:
</pre>
 
=={{header|VisualV Basic .NET(Vlang)}}==
{{trans|C#Go}}
<syntaxhighlight lang="v (vlang)">fn same_digits(nn int, b int) bool {
<lang vbnet>Module Module1
f := nn % b
 
mut n := nn/b
Function sameDigits(ByVal n As Integer, ByVal b As Integer) As Boolean
for n > 0 {
Dim f As Integer = n Mod b : n \= b : While n > 0
Ifif n Mod %b <>!= f Then Return False Else n \= b{
End While : Return Truereturn false
End Function }
n /= b
 
}
Function isBrazilian(ByVal n As Integer) As Boolean
return true
If n < 7 Then Return False
}
If n Mod 2 = 0 Then Return True
For b As Integer = 2 To n - 2
fn is_brazilian(n int) bool {
If sameDigits(n, b) Then Return True
if n < 7 Next : Return False{
End Function return false
}
 
if n%2 == 0 && n >= 8 {
Function isPrime(ByVal n As Integer) As Boolean
Ifreturn n < 2 Then Return Falsetrue
}
If n Mod 2 = 0 Then Return n = 2
for b in 2..n-1 {
If n Mod 3 = 0 Then Return n = 3
Dimif dsame_digits(n, Asb) Integer = 5{
While d * d <=return ntrue
}
If n Mod d = 0 Then Return False Else d += 2
}
If n Mod d = 0 Then Return False Else d += 4
return false
End While : Return True
}
End Function
 
fn is_prime(n int) bool {
Sub Main(args As String())
match true {
For Each kind As String In {" ", " odd ", " prime "}
n < 2 {
Console.WriteLine("First 20{0}Brazilian numbers:", kind)
return false
Dim Limit As Integer = 20, n As Integer = 7
}
Do
n%2 == 0 {
If isBrazilian(n) Then Console.Write("{0} ", n) : Limit -= 1
return n == 2
Select Case kind
}
Case " " : n += 1
n%3 == 0 {
Case " odd " : n += 2
return n == 3
Case " prime " : Do : n += 2 : Loop Until isPrime(n)
}
End Select
else {
Loop While Limit > 0
mut d := 5
Console.Write(vbLf & vbLf)
for d*d <= n {
Next
if n%d == 0 {
End Sub
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}")
}</syntaxhighlight>
 
End Module</lang>
{{out}}
<pre>
<pre>First 20 Brazilian numbers:
First 20 Brazilian numbers:
7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33
 
Line 4,144 ⟶ 5,099:
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>
===Speedier Version===
Based on the C# speedier version, performance is just as good, one billion Brazilian numbers counted in 4 1/2 seconds (on a core i7).
<lang vbnet>Imports System
 
The 100,000th Brazilian number: 110468
Module Module1
' flags:
Const _
PrMk As Integer = 0, ' a number that is prime
SqMk As Integer = 1, ' a number that is the square of a prime number
UpMk As Integer = 2, ' a number that can be factored (aka un-prime)
BrMk As Integer = -2, ' a prime number that is also a Brazilian number
Excp As Integer = 121 ' exception square - the only square prime that is a Brazilian
 
Dim pow As Integer = 9,
max As Integer ' maximum sieve array length
' An upper limit of the required array length can be calculated Like this:
' power of 10 fraction limit actual result
' 1 2 / 1 * 10 = 20 20
' 2 4 / 3 * 100 = 133 132
' 3 6 / 5 * 1000 = 1200 1191
' 4 8 / 7 * 10000 = 11428 11364
' 5 10/ 9 * 100000 = 111111 110468
' 6 12/11 * 1000000 = 1090909 1084566
' 7 14/13 * 10000000 = 10769230 10708453
' 8 16/15 * 100000000 = 106666666 106091516
' 9 18/17 * 1000000000 = 1058823529 1053421821
' powers above 9 are impractical because of the maximum array length in VB.NET,
' which is around the UInt32.MaxValue, Or 4294967295
 
Dim PS As SByte() ' the prime/Brazilian number sieve
' once the sieve is populated, primes are <= 0, non-primes are > 0,
' Brazilian numbers are (< 0) or (> 1)
' 121 is a special case, in the sieve it is marked with the BrMk (-2)
 
' typical sieve of Eratosthenes algorithm
Sub PrimeSieve(ByVal top As Integer)
PS = New SByte(top) {} : Dim i, ii, j As Integer
i = 2 : j = 4 : PS(j) = SqMk : While j < top - 2 : j += 2 : PS(j) = UpMk : End While
i = 3 : j = 9 : PS(j) = SqMk : While j < top - 6 : j += 6 : PS(j) = UpMk : End While
i = 5 : ii = 25 : While ii < top
If PS(i) = PrMk Then
j = (top - i) / i : If (j And 1) = 0 Then j -= 1
Do : If PS(j) = PrMk Then PS(i * j) = UpMk
j -= 2 : Loop While j > i : PS(ii) = SqMk
End If
Do : i += 2 : Loop While PS(i) <> PrMk : ii = i * i
End While
End Sub
 
' consults the sieve and returns whether a number is Brazilian
Function IsBr(ByVal number As Integer) As Boolean
Return Math.Abs(PS(number)) > SqMk
End Function
 
' shows the first few Brazilian numbers of several kinds
Sub FirstFew(ByVal kind As String, ByVal amt As Integer)
Console.WriteLine(vbLf & "The first {0} {1}Brazilian Numbers are:", amt, kind)
Dim i As Integer = 7 : While amt > 0
If IsBr(i) Then amt -= 1 : Console.Write("{0} ", i)
Select Case kind : Case "odd " : i += 2
Case "prime " : Do : i += 2 : Loop While PS(i) <> BrMk OrElse i = Excp
Case Else : i += 1 : End Select : End While : Console.WriteLine()
End Sub
 
' expands a 111_X number into an integer
Function Expand(ByVal NumberOfOnes As Integer, ByVal Base As Integer) As Integer
Dim res As Integer = 1
While NumberOfOnes > 1 AndAlso res < Integer.MaxValue \ Base
res = res * Base + 1 : NumberOfOnes -= 1 : End While
If res > max OrElse res < 0 Then res = 0
Return res
End Function
 
' returns an elapsed time string
Function TS(ByVal fmt As String, ByRef st As DateTime, ByVal Optional reset As Boolean = False) As String
Dim n As DateTime = DateTime.Now,
res As String = String.Format(fmt, (n - st).TotalMilliseconds)
If reset Then st = n
Return res
End Function
 
Sub Main(args As String())
Dim p2 As Integer = pow << 1, primes(6) As Integer, n As Integer,
st As DateTime = DateTime.Now, st0 As DateTime = st,
p10 As Integer = CInt(Math.Pow(10, pow)), p As Integer = 10, cnt As Integer = 0
max = CInt(((CLng((p10)) * p2) / (p2 - 1))) : PrimeSieve(max)
Console.WriteLine(TS("Sieving took {0} ms", st, True))
' make short list of primes before Brazilians are added
n = 3 : For i As Integer = 0 To primes.Length - 1
primes(i) = n : Do : n += 2 : Loop While PS(n) <> 0 : Next
Console.WriteLine(vbLf & "Checking first few prime numbers of sequential ones:" &
vbLf & "ones checked found")
' now check the '111_X' style numbers. many are factorable, but some are prime,
' then re-mark the primes found in the sieve as Brazilian.
' curiously, only the numbers with a prime number of ones will turn out, so
' restricting the search to those saves time. no need to wast time on even numbers of ones,
' or 9 ones, 15 ones, etc...
For Each i As Integer In primes
Console.Write("{0,4}", i) : cnt = 0 : n = 2 : Do
If (n - 1) Mod i <> 0 Then
Dim br As Long = Expand(i, n)
If br > 0 Then
If PS(br) < UpMk Then PS(br) = BrMk : cnt += 1
Else
Console.WriteLine("{0,8}{1,6}", n, cnt) : Exit Do
End If
End If : n += 1 : Loop While True
Next
Console.WriteLine(TS("Adding Brazilian primes to the sieve took {0} ms", st, True))
For Each s As String In ",odd ,prime ".Split(",") : FirstFew(s, 20) : Next
Console.WriteLine(TS(vbLf & "Required output took {0} ms", st, True))
Console.WriteLine(vbLf & "Decade count of Brazilian numbers:")
n = 6 : cnt = 0 : Do : While cnt < p : n += 1 : If IsBr(n) Then cnt += 1
End While
Console.WriteLine("{0,15:n0}th is {1,-15:n0} {2}", cnt, n, TS("time: {0} ms", st))
If p < p10 Then p *= 10 Else Exit Do
Loop While (True) : PS = New SByte(-1) {}
Console.WriteLine(vbLf & "Total elapsed was {0} ms", (DateTime.Now - st0).TotalMilliseconds)
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
 
End Module
</lang>
{{out}}
<pre>Sieving took 2967.834 ms
 
Checking first few prime numbers of sequential ones:
ones checked found
3 32540 3923
5 182 44
7 32 9
11 8 1
13 8 3
17 4 1
19 4 1
Adding Brazilian primes to the sieve took 8.6242 ms
 
The first 20 Brazilian Numbers are:
7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33
 
The first 20 odd Brazilian Numbers are:
7 13 15 21 27 31 33 35 39 43 45 51 55 57 63 65 69 73 75 77
 
The first 20 prime Brazilian Numbers are:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
 
Required output took 2.8256 ms
 
Decade count of Brazilian numbers:
10th is 20 time: 0.0625 ms
100th is 132 time: 0.1156 ms
1,000th is 1,191 time: 0.1499 ms
10,000th is 11,364 time: 0.1986 ms
100,000th is 110,468 time: 0.4081 ms
1,000,000th is 1,084,566 time: 1.9035 ms
10,000,000th is 10,708,453 time: 15.9129 ms
100,000,000th is 106,091,516 time: 149.8814 ms
1,000,000,000th is 1,053,421,821 time: 1412.3526 ms
 
Total elapsed was 4391.7287 ms
</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.
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
var sameDigits = Fn.new { |n, b|
Line 4,364 ⟶ 5,159:
n = n + 1
}
System.print("The 100,000th Brazilian number: %(n-1)")</langsyntaxhighlight>
 
{{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|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">func SameDigits(N, B);
int N, B, F;
[F:= rem(N/B);
N:= N/B;
while N > 0 do
[if rem(N/B) # F then return false;
N:= N/B;
];
return true;
];
 
func IsBrazilian(N);
int N, B;
[if N < 7 then return false;
if rem(N/2) = 0 and N >= 8 then return true;
for B:= 2 to N-2 do
if SameDigits(N, B) then return true;
return false;
];
 
func IsPrime(N); \Return 'true' if N is prime
int N, D;
[if N < 2 then return false;
if (N&1) = 0 then return N = 2;
if rem(N/3) = 0 then return N = 3;
D:= 5;
while D*D <= N do
[if rem(N/D) = 0 then return false;
D:= D+2;
if rem(N/D) = 0 then return false;
D:= D+4;
];
return true;
];
 
int I, C, N, Kinds;
[Kinds:= [" ", " odd ", " prime "];
for I:= 0 to 3-1 do
[Text(0, "First 20"); Text(0, Kinds(I));
Text(0, "Brazilian numbers:^m^j");
C:= 0; N:= 7;
loop [if IsBrazilian(N) then
[IntOut(0, N); ChOut(0, ^ );
C:= C+1;
if C = 20 then
[CrLf(0); CrLf(0); quit];
];
case I of
0: N:= N+1;
1: N:= N+2;
2: repeat N:= N+2 until IsPrime(N)
other [];
];
];
N:= 7; C:= 0;
while C < 100_000 do
[if IsBrazilian(N) then C:= C+1;
N:= N+1;
];
Text(0, "The 100,000th Brazilian number: "); IntOut(0, N-1); CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
Line 4,381 ⟶ 5,253:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn isBrazilian(n){
foreach b in ([2..n-2]){
f,m := n%b, n/b;
Line 4,392 ⟶ 5,264:
False
}
fcn isBrazilianW(n){ isBrazilian(n) and n or Void.Skip }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("First 20 Brazilian numbers:");
[1..].tweak(isBrazilianW).walk(20).println();
 
println("\nFirst 20 odd Brazilian numbers:");
[1..*,2].tweak(isBrazilianW).walk(20).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 4,411 ⟶ 5,283:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
 
println("\nFirst 20 prime Brazilian numbers:");
p:=BI(1);
Walker.zero().tweak('wrap{ p.nextPrime().toInt() })
.tweak(isBrazilianW).walk(20).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 4,422 ⟶ 5,294:
L(7,13,31,43,73,127,157,211,241,307,421,463,601,757,1093,1123,1483,1723,2551,2801)
</pre>
<langsyntaxhighlight lang="zkl">println("The 100,00th Brazilian number: ",
[1..].tweak(isBrazilianW).drop(100_000).value);</langsyntaxhighlight>
{{out}}
<pre>
2,120

edits