Brazilian numbers: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 42:
{{trans|Nim}}
 
<langsyntaxhighlight lang=11l>F isPrime(n)
I n % 2 == 0
R n == 2
Line 91:
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 109:
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 lang=Action!>INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC SameDigits(INT x,b)
Line 178:
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 194:
=={{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:
end_1000000:
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 334:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang=applescript>on isBrazilian(n)
repeat with b from 2 to n - 2
set d to n mod b
Line 394:
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:
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:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f BRAZILIAN_NUMBERS.AWK
# converted from C
Line 510:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 520:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang=c>#include <stdio.h>
 
typedef char bool;
Line 593:
printf("The 100,000th Brazilian number: %d\n", n - 1);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 611:
=={{header|C sharp|C#}}==
{{trans|Go}}
<langsyntaxhighlight lang=csharp>using System;
class Program {
Line 663:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 680:
===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:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}</langsyntaxhighlight>
{{out}}
<pre>Sieving took 3009.2927 ms
Line 819:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang=cpp>#include <iostream>
 
bool sameDigits(int n, int b) {
Line 894:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazillian numbers:
Line 909:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang=d>import std.stdio;
 
bool sameDigits(int n, int b) {
Line 977:
if (kind == "") writefln("The %sth Brazillian number is: %s\n", BigLim + 1, n);
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazillion numbers:
Line 992:
=={{header|Delphi}}==
{{Trans|Go}}
<langsyntaxhighlight lang=Delphi>
program Brazilian_numbers;
 
Line 1,169:
end.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,187:
=={{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:
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:
</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:
;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:
</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:
=={{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 [ 2 + ] lfrom-by "First 20 odd Brazilian numbers:"
lprimes "First 20 prime Brazilian numbers:"
[ print .20-brazilians nl ] 2tri@</langsyntaxhighlight>
{{out}}
<pre>
Line 1,276:
 
=={{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:
0 20 print_brazilian
 
bye</langsyntaxhighlight>
 
{{out}}
Line 1,359:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang=Fortran>
!Constructs a sieve of Brazilian numbers from the definition.
!From the Algol W algorithm, somewhat "Fortranized"
Line 1,533:
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,557:
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight 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
Line 1,599:
Loop While Limit > 0
Next i
Sleep</langsyntaxhighlight>
 
 
=={{header|Go}}==
===Version 1===
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 1,697:
}
fmt.Println("The 100,000th Brazilian number:", n-1)
}</langsyntaxhighlight>
 
{{out}}
Line 1,719:
 
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:
}
fmt.Printf("\nTotal elapsed was %.4f ms\n", toMs(time.Since(st0)))
}</langsyntaxhighlight>
 
{{out}}
Line 1,956:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang=groovy>import org.codehaus.groovy.GroovyBugError
 
class Brazilian {
Line 2,055:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 2,069:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>import Data.Numbers.Primes (primes)
 
isBrazil :: Int -> Bool
Line 2,095:
, "\n"
])
[([], [1 ..]), ("odd", [1,3 ..]), ("prime", primes)]</langsyntaxhighlight>
{{Out}}
<pre>First 20 Brazilians:
Line 2,112:
Not the most beautiful proofs and the theorem about "R*S >= 8, with S+1 > R, are Brazilian" is missing.
 
<langsyntaxhighlight lang=Isabelle>theory Brazilian
imports Main
begin
Line 2,339:
(*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:
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:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 2,488:
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
 
<langsyntaxhighlight lang=jq># Output: a stream of digits, least significant digit first
def to_base($base):
def butlast(s):
Line 2,526:
task(20)
 
</syntaxhighlight>
</lang>
{{out}}
As elsewhere, e.g. [[#Python]].
Line 2,532:
=={{header|Julia}}==
{{trans|Go}}
<langsyntaxhighlight lang=julia>using Primes, Lazy
 
function samedigits(n, b)
Line 2,555:
 
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,563:
 
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,587:
braziliandensities(100000, 1000)
plot(1:1000:1000000, braziliandensities(1000000, 1000))
</langsyntaxhighlight>{{out}}
<pre>
The 10000 th brazilian is 11364.
Line 2,598:
=={{header|Kotlin}}==
{{trans|C#}}
<langsyntaxhighlight lang=scala>fun sameDigits(n: Int, b: Int): Boolean {
var n2 = n
val f = n % b
Line 2,669:
if (kind == "") println("The %,dth Brazilian number is: %,d".format(bigLim + 1, n))
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 2,683:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang=lua>function sameDigits(n,b)
local f = n % b
n = math.floor(n / b)
Line 2,767:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazillion numbers:
Line 2,779:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=mathematica>brazilianQ[n_Integer /; n>6 ] := AnyTrue[
Range[2, n-2],
MatchQ[IntegerDigits[n, #], {x_ ...}] &
Line 2,786:
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}
Line 2,793:
 
=={{header|Nim}}==
<langsyntaxhighlight lang=Nim>proc isPrime(n: Positive): bool =
## Check if a number is prime.
if n mod 2 == 0:
Line 2,861:
inc n, 2
while not n.isPrime():
inc n, 2</langsyntaxhighlight>
 
{{out}}
Line 2,880:
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,171:
 
setlength(isPrime, 0);
end.</langsyntaxhighlight>
{{out}}
<pre>first 20 brazilian numbers
Line 3,264:
{{libheader|ntheory}}
{{trans|Raku}}
<langsyntaxhighlight lang=perl>use strict;
use warnings;
use ntheory qw<is_prime>;
Line 3,300:
print "\n\nFirst $upto prime Brazilian numbers:\n";
$n = 0;
print do { $n < $upto ? (!!is_prime($_) and is_Brazilian($_) and ++$n and "$_ ") : last } for 1 .. Inf;</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 3,313:
=={{header|Phix}}==
{{trans|C}}
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 3,368:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
Not 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.
Line 3,386:
 
=={{header|Python}}==
<langsyntaxhighlight lang=python>'''Brazilian numbers'''
 
from itertools import count, islice
Line 3,500:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 20 Brazilians:
Line 3,513:
=={{header|Racket}}==
 
<langsyntaxhighlight lang=racket>#lang racket
 
(require math/number-theory)
Line 3,540:
(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,554:
{{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,574:
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,587:
=={{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,641:
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,658:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
load "stdlib.ring"
 
Line 3,740:
txt = left(txt,len(txt)-1)
see txt + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,756:
=={{header|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang=ruby>def sameDigits(n,b)
f = n % b
while (n /= b) > 0 do
Line 3,851:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 3,865:
 
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>
fn same_digits(x: u64, base: u64) -> bool {
let f = x % base;
Line 3,930:
println!("\nThe {}th Brazilian number: {}", big_limit, big_result);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,946:
=={{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,053:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 4,067:
===functional===
use Scala 3
<langsyntaxhighlight lang=Scala>object Brazilian extends App {
def oddPrimes =
Line 4,102:
val bigElement = LazyList.from(7).filter(isBrazilian(_)).drop(bigLimit - 1).take(1).head
println(s"brazilian($bigLimit): $bigElement")
}</langsyntaxhighlight>
{{out}}
<pre>brazilian: List(7, 8, 10, 12, 13, 14, 15, 16, 18, 20, 21, 22, 24, 26, 27, 28, 30, 31, 32, 33)
Line 4,112:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>func is_Brazilian_prime(q) {
 
static L = Set()
Line 4,154:
say "\nFirst #{n} prime Brazilian numbers"
say (^Inf -> lazy.grep(is_Brazilian).grep{.is_prime}.first(n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,168:
 
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,183:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Module Module1
 
Function sameDigits(ByVal n As Integer, ByVal b As Integer) As Boolean
Line 4,226:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>First 20 Brazilian numbers:
Line 4,239:
===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).
<langsyntaxhighlight lang=vbnet>Imports System
 
Module Module1
Line 4,359:
 
End Module
</syntaxhighlight>
</lang>
{{out}}
<pre>Sieving took 2967.834 ms
Line 4,402:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang=vlang>fn same_digits(nn int, b int) bool {
f := nn % b
mut n := nn/b
Line 4,499:
}
println("The 100,000th Brazilian number: ${n-1}")
}</langsyntaxhighlight>
 
{{out}}
Line 4,518:
{{trans|Go}}
{{libheader|Wren-math}}
<langsyntaxhighlight lang=ecmascript>import "/math" for Int
 
var sameDigits = Fn.new { |n, b|
Line 4,571:
n = n + 1
}
System.print("The 100,000th Brazilian number: %(n-1)")</langsyntaxhighlight>
 
{{out}}
Line 4,588:
 
=={{header|zkl}}==
<langsyntaxhighlight lang=zkl>fcn isBrazilian(n){
foreach b in ([2..n-2]){
f,m := n%b, n/b;
Line 4,599:
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,618:
 
[[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,629:
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>
10,333

edits