Brazilian numbers: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 38:
:* '''[[oeis:A085104|OEIS:A085104 - Prime Brazilian numbers]]'''
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F isPrime(n)
I n % 2 == 0
R n == 2
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}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC SameDigits(INT x,b)
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.
<syntaxhighlight 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 331 ⟶ 328:
1000000th Brazilian number: 1084566
</pre>
 
=={{header|AppleScript}}==
 
<syntaxhighlight lang="applescript">on isBrazilian(n)
repeat with b from 2 to n - 2
set d to n mod b
Line 398 ⟶ 394:
{{output}}
 
<syntaxhighlight 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:
Line 404 ⟶ 400:
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"</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">brazilian?: function [n][
if n < 7 -> return false
if zero? and n 1 -> return true
Line 444 ⟶ 439:
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|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f BRAZILIAN_NUMBERS.AWK
# converted from C
Line 517 ⟶ 511:
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|C}}==
{{trans|Go}}
<syntaxhighlight lang="c">#include <stdio.h>
 
typedef char bool;
Line 608 ⟶ 601:
The 100,000th Brazilian number: 110468
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Go}}
<syntaxhighlight lang="csharp">using System;
class Program {
Line 680 ⟶ 672:
===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.
<syntaxhighlight lang="csharp">using System;
 
class Program
Line 816 ⟶ 808:
 
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}}
<syntaxhighlight lang="cpp">#include <iostream>
 
bool sameDigits(int n, int b) {
Line 906 ⟶ 897:
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#}}
<syntaxhighlight lang="d">import std.stdio;
 
bool sameDigits(int n, int b) {
Line 989 ⟶ 979:
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"delphi">
program Brazilian_numbers;
 
Line 1,184 ⟶ 1,173:
The 100,000th Brazilian number: 110468
</pre>
 
=={{header|F_Sharp|F#}}==
===The functions===
<syntaxhighlight 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,197 ⟶ 1,185:
===The Tasks===
;the first 20 Brazilian numbers
<syntaxhighlight lang="fsharp">
Brazilian() |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
Line 1,205 ⟶ 1,193:
</pre>
;the first 20 odd Brazilian numbers
<syntaxhighlight lang="fsharp">
Brazilian() |> Seq.filter(fun n->n%2=1) |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
Line 1,214 ⟶ 1,202:
;the first 20 prime Brazilian numbers
Using [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
Brazilian() |> Seq.filter isPrime |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
Line 1,222 ⟶ 1,210:
</pre>
;finally that which the crowd really want to know: What is the 100,000<sup>th</sup> Brazilian number?
<syntaxhighlight lang="fsharp">
printfn "%d" (Seq.item 99999 Brazilian)
</syntaxhighlight>
Line 1,230 ⟶ 1,218:
</pre>
So up to 100,000 ~10% of numbers are non-Brazilian. The millionth Brazilian is 1084566 so less than 10% are non-Brazilian. Large non-Brazilians seem to be rare.
 
=={{header|Factor}}==
{{works with|Factor|0.99 development release 2019-07-10}}
<syntaxhighlight 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,266 ⟶ 1,253:
{ 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}}==
<syntaxhighlight lang="forth">: prime? ( n -- flag )
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
Line 1,357 ⟶ 1,335:
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"fortran">
!Constructs a sieve of Brazilian numbers from the definition.
!From the Algol W algorithm, somewhat "Fortranized"
Line 1,554 ⟶ 1,531:
 
</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
Line 1,600 ⟶ 1,576:
Next i
Sleep</syntaxhighlight>
=={{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|Go}}==
===Version 1===
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 1,719 ⟶ 1,700:
 
Running a bit quicker than the .NET versions though not due to any further improvements on my part.
<syntaxhighlight lang="go">package main
 
import (
Line 1,953 ⟶ 1,934:
Total elapsed was 3249.0197 ms
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">import org.codehaus.groovy.GroovyBugError
 
class Brazilian {
Line 2,067 ⟶ 2,047:
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}}==
<syntaxhighlight lang="haskell">import Data.Numbers.Primes (primes)
 
isBrazil :: Int -> Bool
Line 2,105 ⟶ 2,084:
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,089:
Not the most beautiful proofs and the theorem about "R*S >= 8, with S+1 > R, are Brazilian" is missing.
 
<syntaxhighlight lang=Isabelle"isabelle">theory Brazilian
imports Main
begin
Line 2,340 ⟶ 2,317:
 
end</syntaxhighlight>
 
=={{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,370 ⟶ 2,346:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.math.BigInteger;
import java.util.List;
 
Line 2,481 ⟶ 2,456:
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}}
Line 2,488 ⟶ 2,462:
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):
Line 2,529 ⟶ 2,503:
{{out}}
As elsewhere, e.g. [[#Python]].
 
=={{header|Julia}}==
{{trans|Go}}
<syntaxhighlight lang="julia">using Primes, Lazy
 
function samedigits(n, b)
Line 2,563 ⟶ 2,536:
 
There has been some discussion of larger numbers in the sequence. See below:
<syntaxhighlight lang="julia">function braziliandensities(N, interval)
count, intervalcount, icount = 0, 0, 0
intervalcounts = Int[]
Line 2,595 ⟶ 2,568:
 
[http://alahonua.com/temp/newplot.png link plot png]
 
=={{header|Kotlin}}==
{{trans|C#}}
<syntaxhighlight lang="scala">fun sameDigits(n: Int, b: Int): Boolean {
var n2 = n
val f = n % b
Line 2,680 ⟶ 2,652:
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}}
<syntaxhighlight lang="lua">function sameDigits(n,b)
local f = n % b
n = math.floor(n / b)
Line 2,777 ⟶ 2,748:
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}}==
<syntaxhighlight lang="mathematica">brazilianQ[n_Integer /; n>6 ] := AnyTrue[
Range[2, n-2],
MatchQ[IntegerDigits[n, #], {x_ ...}] &
Line 2,791 ⟶ 2,761:
{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|Nim}}==
<syntaxhighlight lang=Nim"nim">proc isPrime(n: Positive): bool =
## Check if a number is prime.
if n mod 2 == 0:
Line 2,873 ⟶ 2,842:
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|Pascal}}==
{{works with|Free Pascal}}
Line 2,880 ⟶ 2,848:
extreme reduced runtime time for space.<BR>
At the end only primes and square of primes need to be tested, all others are Brazilian.
<syntaxhighlight lang="pascal">program brazilianNumbers;
 
{$IFDEF FPC}
Line 3,260 ⟶ 3,228:
sys 0m0,157s
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use ntheory qw<is_prime>;
Line 3,310 ⟶ 3,277:
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"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,384 ⟶ 3,350:
"52.8s"
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">'''Brazilian numbers'''
 
from itertools import count, islice
Line 3,510 ⟶ 3,475:
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|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 3,549 ⟶ 3,513:
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" line>multi is-Brazilian (Int $n where $n %% 2 && $n > 6) { True }
 
multi is-Brazilian (Int $n) {
Line 3,584 ⟶ 3,547:
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,656 ⟶ 3,618:
110468
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 3,753 ⟶ 3,714:
done...
</pre>
 
=={{header|Ruby}}==
{{trans|C++}}
<syntaxhighlight lang="ruby">def sameDigits(n,b)
f = n % b
while (n /= b) > 0 do
Line 3,863 ⟶ 3,823:
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|Rust}}==
<syntaxhighlight lang="rust">
fn same_digits(x: u64, base: u64) -> bool {
let f = x % base;
Line 3,946 ⟶ 3,905:
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight 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,067 ⟶ 4,026:
===functional===
use Scala 3
<syntaxhighlight lang=Scala"scala">object Brazilian extends App {
def oddPrimes =
Line 4,110 ⟶ 4,069:
brazilian(100000): 110468
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func is_Brazilian_prime(q) {
 
static L = Set()
Line 4,168 ⟶ 4,126:
 
Extra:
<syntaxhighlight lang="ruby">for n in (1..6) {
say ("#{10**n->commify}th Brazilian number = ", is_Brazilian.nth(10**n))
}</syntaxhighlight>
Line 4,180 ⟶ 4,138:
1,000,000th Brazilian number = 1084566
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function sameDigits(ByVal n As Integer, ByVal b As Integer) As Boolean
Line 4,239 ⟶ 4,196:
===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
Line 4,399 ⟶ 4,356:
</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|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="vlang">fn same_digits(nn int, b int) bool {
f := nn % b
mut n := nn/b
Line 4,514 ⟶ 4,470:
The 100,000th Brazilian number: 110468
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascript">import "/math" for Int
 
var sameDigits = Fn.new { |n, b|
Line 4,586 ⟶ 4,541:
The 100,000th Brazilian number: 110468
</pre>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn isBrazilian(n){
foreach b in ([2..n-2]){
f,m := n%b, n/b;
Line 4,600 ⟶ 4,554:
}
fcn isBrazilianW(n){ isBrazilian(n) and n or Void.Skip }</syntaxhighlight>
<syntaxhighlight lang="zkl">println("First 20 Brazilian numbers:");
[1..].tweak(isBrazilianW).walk(20).println();
 
Line 4,618 ⟶ 4,572:
 
[[Extensible prime generator#zkl]] could be used instead.
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
 
println("\nFirst 20 prime Brazilian numbers:");
Line 4,629 ⟶ 4,583:
L(7,13,31,43,73,127,157,211,241,307,421,463,601,757,1093,1123,1483,1723,2551,2801)
</pre>
<syntaxhighlight lang="zkl">println("The 100,00th Brazilian number: ",
[1..].tweak(isBrazilianW).drop(100_000).value);</syntaxhighlight>
{{out}}
10,327

edits