Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Blum-integer/00-META.yaml
Normal file
2
Task/Blum-integer/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Blum_integer
|
||||
23
Task/Blum-integer/00-TASK.txt
Normal file
23
Task/Blum-integer/00-TASK.txt
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
;Definition
|
||||
A positive integer '''n''' is a '''Blum integer''' if ''n = p x q'' is a semi-prime for which ''p'' and ''q'' are distinct primes congruent to 3 mod 4. In other words, ''p'' and ''q'' must be of the form 4''t'' + 3 where ''t'' is some non-negative integer.
|
||||
<br>
|
||||
;Example
|
||||
21 is a Blum integer because it has two prime factors: 3 (= 4 x 0 + 3) and 7 (= 4 x 1 + 3).
|
||||
|
||||
;Task
|
||||
Find and show on this page the first '''50''' Blum integers.
|
||||
|
||||
Also show the '''26,828'''th.
|
||||
<br>
|
||||
;Stretch
|
||||
Find and show the '''100,000'''th, '''200,000'''th, '''300,000'''th and '''400,000'''th Blum integers.
|
||||
|
||||
For the first '''400,000''' Blum integers, show the percentage distribution by final decimal digit (to 3 decimal places). Clearly, such integers can only end in 1, 3, 7 or 9.
|
||||
|
||||
;Related task
|
||||
* [[Semiprime]]
|
||||
|
||||
;References
|
||||
* Wikipedia article [[wp:Blum_integer|Blum integer]]
|
||||
* OEIS sequence [[oeis:A016105|A016105: Blum integers]]
|
||||
<br>
|
||||
90
Task/Blum-integer/ALGOL-68/blum-integer.alg
Normal file
90
Task/Blum-integer/ALGOL-68/blum-integer.alg
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
BEGIN # find Blum integers, semi-primes where both factors are 3 mod 4 #
|
||||
# and the factors are distinct #
|
||||
INT max number = 10 000 000; # maximum possible Blum we will consider #
|
||||
[ 1 : max number ]INT upfc; # table of unique prime factor counts #
|
||||
[ 1 : max number ]INT lpf; # table of last prime factors #
|
||||
FOR i TO UPB upfc DO upfc[ i ] := 0; lpf[ i ] := 1 OD;
|
||||
FOR i FROM 2 TO UPB upfc OVER 2 DO
|
||||
IF upfc[ i ] = 0 THEN
|
||||
FOR j FROM i + i BY i TO UPB upfc DO
|
||||
upfc[ j ] +:= 1;
|
||||
lpf[ j ] := i
|
||||
OD
|
||||
FI
|
||||
OD;
|
||||
# returns TRUE if n is a Blum integer, FALSE otherwise #
|
||||
PROC is blum = ( INT n )BOOL:
|
||||
IF n < 21 THEN FALSE # the lowest primes modulo 4 that are 3 are #
|
||||
# 3 & 7, so 21 is the lowest possible number #
|
||||
ELIF NOT ODD n THEN FALSE
|
||||
ELSE
|
||||
INT v := n;
|
||||
INT f count := 0;
|
||||
INT f := 3;
|
||||
WHILE f <= v AND f count < 3 DO
|
||||
IF v MOD f = 0 THEN
|
||||
IF f MOD 4 /= 3 THEN
|
||||
f count := 9 # the prime factor mod 4 isn't 3 #
|
||||
ELSE
|
||||
v OVERAB f;
|
||||
f count +:= 1
|
||||
FI;
|
||||
IF v MOD f = 0 THEN
|
||||
f count := 9 # f is not a distinct factor of n #
|
||||
FI
|
||||
FI;
|
||||
f +:= 2
|
||||
OD;
|
||||
f count = 2
|
||||
FI # is blum # ;
|
||||
|
||||
# show various Blum integers #
|
||||
print( ( "The first 50 Blum integers:", newline ) );
|
||||
INT b count := 0;
|
||||
[ 0 : 9 ]INT last count := []INT( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )[ AT 0 ];
|
||||
FOR i FROM 21 BY 4 WHILE b count < 400 000 DO
|
||||
# Blum integers must be 1 modulo 4 #
|
||||
IF b count < 50 THEN
|
||||
IF is blum( i ) THEN
|
||||
b count +:= 1;
|
||||
last count[ i MOD 10 ] +:= 1;
|
||||
print( ( whole( i, -4 ) ) );
|
||||
IF b count MOD 10 = 0 THEN print( ( newline ) ) FI
|
||||
FI
|
||||
ELIF upfc[ i ] = 2 THEN
|
||||
# two prime factors - could be a Blum integer #
|
||||
IF lpf[ i ] MOD 4 = 3 THEN
|
||||
# the last prime factor mod 4 is three #
|
||||
IF upfc[ i OVER lpf[ i ] ] = 0 THEN
|
||||
# and the other prime factor is unique (e.g. not 3^3) #
|
||||
b count +:= 1;
|
||||
last count[ i MOD 10 ] +:= 1;
|
||||
IF b count = 26 828
|
||||
OR b count = 100 000
|
||||
OR b count = 200 000
|
||||
OR b count = 300 000
|
||||
OR b count = 400 000
|
||||
THEN
|
||||
print( ( "The ", whole( b count, -6 )
|
||||
, "th Blum integer is ", whole( i, -11 )
|
||||
, newline
|
||||
)
|
||||
)
|
||||
FI
|
||||
FI
|
||||
FI
|
||||
FI
|
||||
OD;
|
||||
# show some statistics of the last digits #
|
||||
print( ( "For Blum integers up to ", whole( b count, 0 ), ":", newline ) );
|
||||
FOR i FROM LWB last count TO UPB last count DO
|
||||
IF last count[ i ] /= 0 THEN
|
||||
print( ( " ", fixed( ( last count[ i ] * 100 ) / b count, -8, 3 )
|
||||
, "% end with ", whole( i, 0 )
|
||||
, newline
|
||||
)
|
||||
)
|
||||
FI
|
||||
OD
|
||||
|
||||
END
|
||||
71
Task/Blum-integer/C/blum-integer.c
Normal file
71
Task/Blum-integer/C/blum-integer.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <locale.h>
|
||||
|
||||
int inc[8] = {4, 2, 4, 2, 4, 6, 2, 6};
|
||||
|
||||
bool isPrime(int n) {
|
||||
if (n < 2) return false;
|
||||
if (n%2 == 0) return n == 2;
|
||||
if (n%3 == 0) return n == 3;
|
||||
int d = 5;
|
||||
while (d*d <= n) {
|
||||
if (n%d == 0) return false;
|
||||
d += 2;
|
||||
if (n%d == 0) return false;
|
||||
d += 4;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Assumes n is odd.
|
||||
int firstPrimeFactor(int n) {
|
||||
if (n == 1) return 1;
|
||||
if (!(n%3)) return 3;
|
||||
if (!(n%5)) return 5;
|
||||
for (int k = 7, i = 0; k*k <= n; ) {
|
||||
if (!(n%k)) {
|
||||
return k;
|
||||
} else {
|
||||
k += inc[i];
|
||||
i = (i + 1) % 8;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int i = 1, j, bc = 0, p, q;
|
||||
int blum[50], counts[4] = {0}, digits[4] = {1, 3, 5, 7};
|
||||
setlocale(LC_NUMERIC, "");
|
||||
while (true) {
|
||||
p = firstPrimeFactor(i);
|
||||
if (p % 4 == 3) {
|
||||
q = i / p;
|
||||
if (q != p && q % 4 == 3 && isPrime(q)) {
|
||||
if (bc < 50) blum[bc] = i;
|
||||
++counts[i % 10 / 3];
|
||||
++bc;
|
||||
if (bc == 50) {
|
||||
printf("First 50 Blum integers:\n");
|
||||
for (j = 0; j < 50; ++j) {
|
||||
printf("%3d ", blum[j]);
|
||||
if (!((j+1) % 10)) printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
} else if (bc == 26828 || !(bc % 100000)) {
|
||||
printf("The %'7dth Blum integer is: %'9d\n", bc, i);
|
||||
if (bc == 400000) {
|
||||
printf("\n%% distribution of the first 400,000 Blum integers:\n");
|
||||
for (j = 0; j < 4; ++j) {
|
||||
printf(" %6.3f%% end in %d\n", counts[j]/4000.0, digits[j]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
i += (i % 5 == 3) ? 4 : 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
159
Task/Blum-integer/Free-Pascal/blum-integer.pas
Normal file
159
Task/Blum-integer/Free-Pascal/blum-integer.pas
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
program BlumInteger;
|
||||
{$IFDEF FPC} {$MODE DELPHI}{$Optimization ON,ALL} {$ENDIF}
|
||||
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
|
||||
{
|
||||
// for commatize = Numb2USA(IntToStr(n))
|
||||
uses
|
||||
sysutils, //IntToStr
|
||||
strutils; //Numb2USA
|
||||
}
|
||||
const
|
||||
LIMIT = 10*1000*1000;// >750
|
||||
type
|
||||
tP4n3 = array of Uint32;
|
||||
|
||||
function CommaUint(n : Uint64):AnsiString;
|
||||
//commatize only positive Integers
|
||||
var
|
||||
fromIdx,toIdx :Int32;
|
||||
pRes : pChar;
|
||||
Begin
|
||||
str(n,result);
|
||||
fromIdx := length(result);
|
||||
toIdx := fromIdx-1;
|
||||
if toIdx < 3 then
|
||||
exit;
|
||||
|
||||
toIdx := 4*(toIdx DIV 3)+toIdx MOD 3 +1 ;
|
||||
setlength(result,toIdx);
|
||||
pRes := @result[1];
|
||||
dec(pRes);
|
||||
repeat
|
||||
pRes[toIdx] := pRes[FromIdx];
|
||||
pRes[toIdx-1] := pRes[FromIdx-1];
|
||||
pRes[toIdx-2] := pRes[FromIdx-2];
|
||||
pRes[toIdx-3] := ',';
|
||||
dec(toIdx,4);
|
||||
dec(FromIdx,3);
|
||||
until FromIdx<=3;
|
||||
while fromIdx>=1 do
|
||||
Begin
|
||||
pRes[toIdx] := pRes[FromIdx];
|
||||
dec(toIdx);
|
||||
dec(fromIdx);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure Sieve4n_3_Primes(Limit:Uint32;var P4n3:tP4n3);
|
||||
var
|
||||
sieve : array of byte;
|
||||
BlPrCnt,idx,n,j: Uint32;
|
||||
begin
|
||||
//DIV 3 -> smallest factor of Blum Integer
|
||||
n := (LIMIT DIV 3 -3) DIV 4+ 1;
|
||||
setlength(sieve,n);
|
||||
setlength(P4n3,n);
|
||||
|
||||
BlPrCnt:= 0;
|
||||
idx := 0;
|
||||
repeat
|
||||
if sieve[idx]= 0 then
|
||||
begin
|
||||
n := idx*4+3;
|
||||
P4n3[BlPrCnt] := n;
|
||||
inc(BlPrCnt);
|
||||
j := idx+n;
|
||||
if j > High(sieve) then
|
||||
break;
|
||||
while j <= High(sieve) do
|
||||
begin
|
||||
sieve[j] := 1;
|
||||
inc(j,n);
|
||||
end;
|
||||
end;
|
||||
inc(idx);
|
||||
until idx>High(sieve);
|
||||
//collect the rest
|
||||
for idx := idx+1 to High(sieve) do
|
||||
if sieve[idx]=0 then
|
||||
Begin
|
||||
P4n3[BlPrCnt] := idx*4+3;
|
||||
inc(BlPrCnt);
|
||||
end;
|
||||
setlength(P4n3,BlPrCnt);
|
||||
setlength(sieve,0);
|
||||
end;
|
||||
|
||||
var
|
||||
BlumField : array[0..LIMIT] of boolean;
|
||||
BlumPrimes : tP4n3;
|
||||
EndDigit : array[0..9] of Uint32;
|
||||
k : Uint64;
|
||||
n,idx,j,P4n3Cnt : Uint32;
|
||||
begin
|
||||
Sieve4n_3_Primes(Limit,BlumPrimes);
|
||||
P4n3Cnt := length(BlumPrimes);
|
||||
writeln('There are ',CommaUint(P4n3Cnt),' needed primes 4*n+3 to Limit ',CommaUint(LIMIT));
|
||||
dec(P4n3Cnt);
|
||||
writeln;
|
||||
|
||||
//generate Blum-Integers
|
||||
For idx := 0 to P4n3Cnt do
|
||||
Begin
|
||||
n := BlumPrimes[idx];
|
||||
For j := idx+1 to P4n3Cnt do
|
||||
Begin
|
||||
k := n*BlumPrimes[j];
|
||||
if k>LIMIT then
|
||||
BREAK;
|
||||
BlumField[k] := true;
|
||||
end;
|
||||
end;
|
||||
|
||||
writeln('First 50 Blum-Integers ');
|
||||
idx :=0;
|
||||
j := 0 ;
|
||||
repeat
|
||||
while (idx<LIMIT) AND Not(BlumField[idx]) do
|
||||
inc(idx);
|
||||
if idx = LIMIT then
|
||||
BREAK;
|
||||
if j mod 10 = 0 then
|
||||
writeln;
|
||||
write(idx:5);
|
||||
inc(j);
|
||||
inc(idx);
|
||||
until j >= 50;
|
||||
Writeln(#13,#10);
|
||||
|
||||
//count and calc and summate decimal digit
|
||||
writeln(' relative occurence of digit');
|
||||
writeln(' n.th |Blum-Integer|Digit: 1 3 7 9');
|
||||
idx :=0;
|
||||
j := 0 ;
|
||||
n := 0;
|
||||
k := 26828;
|
||||
repeat
|
||||
while (idx<LIMIT) AND Not(BlumField[idx]) do
|
||||
inc(idx);
|
||||
if idx = LIMIT then
|
||||
BREAK;
|
||||
//count last decimal digit
|
||||
inc(EndDigit[idx MOD 10]);
|
||||
inc(j);
|
||||
if j = k then
|
||||
begin
|
||||
write(CommaUint(j):10,' | ',CommaUint(idx):11,'| ');
|
||||
write(EndDigit[1]/j*100:7:3,'% |');
|
||||
write(EndDigit[3]/j*100:7:3,'% |');
|
||||
write(EndDigit[7]/j*100:7:3,'% |');
|
||||
writeln(EndDigit[9]/j*100:7:3,'%');
|
||||
if k < 100000 then
|
||||
k := 100000
|
||||
else
|
||||
k += 100000;
|
||||
end;
|
||||
inc(idx);
|
||||
until j >= 400000;
|
||||
Writeln;
|
||||
end.
|
||||
39
Task/Blum-integer/FreeBASIC/blum-integer.basic
Normal file
39
Task/Blum-integer/FreeBASIC/blum-integer.basic
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
Dim Shared As Uinteger Prime1
|
||||
Dim As Uinteger n = 3, c = 0, Prime2
|
||||
|
||||
Function isSemiprime(n As Uinteger) As Boolean
|
||||
Dim As Uinteger d = 3, c = 0
|
||||
While d*d <= n
|
||||
While n Mod d = 0
|
||||
If c = 2 Then Return False
|
||||
n /= d
|
||||
c += 1
|
||||
Wend
|
||||
d += 2
|
||||
Wend
|
||||
Prime1 = n
|
||||
Return c = 1
|
||||
End Function
|
||||
|
||||
Print "The first 50 Blum integers:"
|
||||
Do
|
||||
If isSemiprime(n) Then
|
||||
If Prime1 Mod 4 = 3 Then
|
||||
Prime2 = n / Prime1
|
||||
If (Prime2 <> Prime1) And (Prime2 Mod 4 = 3) Then
|
||||
c += 1
|
||||
If c <= 50 Then
|
||||
Print Using "####"; n;
|
||||
If c Mod 10 = 0 Then Print
|
||||
End If
|
||||
If c >= 26828 Then
|
||||
Print !"\nThe 26828th Blum integer is: " ; n
|
||||
Exit Do
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
n += 2
|
||||
Loop
|
||||
|
||||
Sleep
|
||||
70
Task/Blum-integer/Go/blum-integer.go
Normal file
70
Task/Blum-integer/Go/blum-integer.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"rcu"
|
||||
)
|
||||
|
||||
var inc = []int{4, 2, 4, 2, 4, 6, 2, 6}
|
||||
|
||||
// Assumes n is odd.
|
||||
func firstPrimeFactor(n int) int {
|
||||
if n == 1 {
|
||||
return 1
|
||||
}
|
||||
if n%3 == 0 {
|
||||
return 3
|
||||
}
|
||||
if n%5 == 0 {
|
||||
return 5
|
||||
}
|
||||
for k, i := 7, 0; k*k <= n; {
|
||||
if n%k == 0 {
|
||||
return k
|
||||
} else {
|
||||
k += inc[i]
|
||||
i = (i + 1) % 8
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func main() {
|
||||
blum := make([]int, 50)
|
||||
bc := 0
|
||||
counts := make([]int, 4)
|
||||
i := 1
|
||||
for {
|
||||
p := firstPrimeFactor(i)
|
||||
if p%4 == 3 {
|
||||
q := i / p
|
||||
if q != p && q%4 == 3 && rcu.IsPrime(q) {
|
||||
if bc < 50 {
|
||||
blum[bc] = i
|
||||
}
|
||||
counts[i%10/3]++
|
||||
bc++
|
||||
if bc == 50 {
|
||||
fmt.Println("First 50 Blum integers:")
|
||||
rcu.PrintTable(blum, 10, 3, false)
|
||||
fmt.Println()
|
||||
} else if bc == 26828 || bc%100000 == 0 {
|
||||
fmt.Printf("The %7sth Blum integer is: %9s\n", rcu.Commatize(bc), rcu.Commatize(i))
|
||||
if bc == 400000 {
|
||||
fmt.Println("\n% distribution of the first 400,000 Blum integers:")
|
||||
digits := []int{1, 3, 7, 9}
|
||||
for j := 0; j < 4; j++ {
|
||||
fmt.Printf(" %6.3f%% end in %d\n", float64(counts[j])/4000, digits[j])
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if i%5 == 3 {
|
||||
i += 4
|
||||
} else {
|
||||
i += 2
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Task/Blum-integer/J/blum-integer-1.j
Normal file
17
Task/Blum-integer/J/blum-integer-1.j
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
isblum=: {{
|
||||
ab=. q: y
|
||||
if. 2= #ab do.
|
||||
if. </ab do.
|
||||
*./3=4|ab
|
||||
else. 0 end.
|
||||
else. 0 end.
|
||||
}}"0
|
||||
|
||||
blumseq=: {{
|
||||
r=. (#~ isblum) }.i.b=. 1e4
|
||||
while. y>#r do.
|
||||
r=. r, (#~ isblum) b+i.1e4
|
||||
b=. b+1e4
|
||||
end.
|
||||
y{.r
|
||||
}}
|
||||
8
Task/Blum-integer/J/blum-integer-2.j
Normal file
8
Task/Blum-integer/J/blum-integer-2.j
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
5 10$blumseq 50
|
||||
21 33 57 69 77 93 129 133 141 161
|
||||
177 201 209 213 217 237 249 253 301 309
|
||||
321 329 341 381 393 413 417 437 453 469
|
||||
473 489 497 501 517 537 553 573 581 589
|
||||
597 633 649 669 681 713 717 721 737 749
|
||||
{: blumseq 26828
|
||||
524273
|
||||
16
Task/Blum-integer/J/blum-integer-3.j
Normal file
16
Task/Blum-integer/J/blum-integer-3.j
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
B=: blumseq 4e5
|
||||
{:1e5{.B
|
||||
2075217
|
||||
{:2e5{.B
|
||||
4275533
|
||||
{:3e5{.B
|
||||
6521629
|
||||
{:4e5{.B
|
||||
8802377
|
||||
{:B
|
||||
8802377
|
||||
(~.,. 4e3 %~ #/.~) 10|B
|
||||
1 25.0012
|
||||
3 25.0167
|
||||
7 24.9973
|
||||
9 24.9847
|
||||
107
Task/Blum-integer/Jq/blum-integer.jq
Normal file
107
Task/Blum-integer/Jq/blum-integer.jq
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
### Generic utilities
|
||||
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
|
||||
|
||||
# tabular print
|
||||
def tprint(columns; wide):
|
||||
reduce _nwise(columns) as $row ("";
|
||||
. + ($row|map(lpad(wide)) | join(" ")) + "\n" );
|
||||
|
||||
### Primes
|
||||
def is_prime:
|
||||
. as $n
|
||||
| if ($n < 2) then false
|
||||
elif ($n % 2 == 0) then $n == 2
|
||||
elif ($n % 3 == 0) then $n == 3
|
||||
elif ($n % 5 == 0) then $n == 5
|
||||
elif ($n % 7 == 0) then $n == 7
|
||||
elif ($n % 11 == 0) then $n == 11
|
||||
elif ($n % 13 == 0) then $n == 13
|
||||
elif ($n % 17 == 0) then $n == 17
|
||||
elif ($n % 19 == 0) then $n == 19
|
||||
else sqrt as $s
|
||||
| 23
|
||||
| until( . > $s or ($n % . == 0); . + 2)
|
||||
| . > $s
|
||||
end;
|
||||
|
||||
def primes: 2, (range(3;infinite;2) | select(is_prime));
|
||||
|
||||
# input: the number to be tested
|
||||
def isprime($smalls):
|
||||
if . < 2 then false
|
||||
else sqrt as $s
|
||||
| (first( $smalls[] as $p
|
||||
| if . == $p then 1
|
||||
elif . % $p == 0 then 0
|
||||
elif $p > $s then 1
|
||||
else empty
|
||||
end) // null) as $result
|
||||
| if $result then $result == 1
|
||||
else ($smalls[-1] + 2)
|
||||
| until( . > $s or ($n % . == 0); . + 2)
|
||||
| . > $s
|
||||
end
|
||||
end;
|
||||
|
||||
# Assumes n is odd.
|
||||
def firstPrimeFactor:
|
||||
if (. == 1) then 1
|
||||
elif (. % 3 == 0) then 3
|
||||
elif (. % 5 == 0) then 5
|
||||
else . as $n
|
||||
| [4, 2, 4, 2, 4, 6, 2, 6] as $inc
|
||||
| { k: 7,
|
||||
i: 0 }
|
||||
| ($n | sqrt) as $s
|
||||
| until (.k > $s or .done;
|
||||
if $n % .k == 0
|
||||
then .done = true
|
||||
else .k += $inc[.i]
|
||||
| .i = (.i + 1) % 8
|
||||
end )
|
||||
| if .done then .k else $n end
|
||||
end ;
|
||||
|
||||
### Blum integers
|
||||
|
||||
# Number of small primes to pre-compute
|
||||
def task($numberOfSmallPrimes):
|
||||
[limit($numberOfSmallPrimes; primes)] as $smalls
|
||||
| { blum: [],
|
||||
bc:0,
|
||||
counts: { "1": 0, "3": 0, "7": 0, "9": 0 },
|
||||
i: 1 }
|
||||
| label $out
|
||||
| foreach range(0; infinite) as $_ (.;
|
||||
(.i|firstPrimeFactor) as $p
|
||||
| .j = null
|
||||
| if ($p % 4 == 3)
|
||||
then (.i / $p) as $q
|
||||
| if $q != $p and ($q % 4 == 3) and ($q | isprime($smalls))
|
||||
then if (.bc < 50) then .blum[.bc] = .i else . end
|
||||
| .counts[(.i % 10) | tostring] += 1
|
||||
| .bc += 1
|
||||
| .j = .i
|
||||
else .
|
||||
end
|
||||
else .
|
||||
end
|
||||
| .i |= if (. % 5 == 3) then . + 4 else . + 2 end;
|
||||
|
||||
select(.j)
|
||||
| if (.bc == 50)
|
||||
then "First 50 Blum integers:",
|
||||
(.blum | tprint(10; 3) )
|
||||
elif .bc == 26828 or .bc % 1e5 == 0
|
||||
then "The \(.bc) Blum integer is: \(.j)",
|
||||
if .bc == 400000
|
||||
then "\n% distribution of the first 400,000 Blum integers:",
|
||||
((.counts|keys_unsorted[]) as $k
|
||||
| " \( .counts[$k] / 4000 )% end in \($k)"),
|
||||
break $out
|
||||
else empty
|
||||
end
|
||||
else empty
|
||||
end);
|
||||
|
||||
task(10000)
|
||||
21
Task/Blum-integer/Julia/blum-integer.julia
Normal file
21
Task/Blum-integer/Julia/blum-integer.julia
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using Formatting, Primes
|
||||
|
||||
function isblum(n)
|
||||
pe = factor(n).pe
|
||||
return length(pe) == 2 && all(p -> p[2] == 1 && p[1] % 4 == 3, pe)
|
||||
end
|
||||
|
||||
const blum400k = @view (filter(isblum, 1:9_000_000))[1:400_000]
|
||||
|
||||
println("First 50 Blum integers:")
|
||||
foreach(p -> print(rpad(p[2], 4), p[1] % 10 == 0 ? "\n" : ""), enumerate(blum400k[1:50]))
|
||||
|
||||
for idx in [26_828, 100_000, 200_000, 300_000, 400_000]
|
||||
println("\nThe $(format(idx, commas = true))th Blum number is ",
|
||||
format(blum400k[idx], commas = true), ".")
|
||||
end
|
||||
|
||||
println("\n% distribution of the first 400,000 Blum integers is:")
|
||||
for d in [1, 3, 7, 9]
|
||||
println(lpad(round(count(x -> x % 10 == d, blum400k) / 4000, digits=3), 8), "% end in $d")
|
||||
end
|
||||
60
Task/Blum-integer/Nim/blum-integer.nim
Normal file
60
Task/Blum-integer/Nim/blum-integer.nim
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import std/[strformat, tables]
|
||||
|
||||
func isPrime(n: Natural): bool =
|
||||
## Return "true" is "n" is prime.
|
||||
if n < 2: return false
|
||||
if (n and 1) == 0: return n == 2
|
||||
if n mod 3 == 0: return n == 3
|
||||
var d = 5
|
||||
var step = 2
|
||||
while d * d <= n:
|
||||
if n mod d == 0:
|
||||
return false
|
||||
inc d, step
|
||||
step = 6 - step
|
||||
return true
|
||||
|
||||
const Inc = [4, 2, 4, 2, 4, 6, 2, 6]
|
||||
|
||||
func firstPrimeFactor(n: Positive): int =
|
||||
## Return the first prime factor.
|
||||
## Assuming "n" is odd.
|
||||
if n == 1: return 1
|
||||
if n mod 3 == 0: return 3
|
||||
if n mod 5 == 0: return 5
|
||||
var k = 7
|
||||
var i = 0
|
||||
while k * k <= n:
|
||||
if n mod k == 0:
|
||||
return k
|
||||
k += Inc[i]
|
||||
i = (i + 1) and 7
|
||||
return n
|
||||
|
||||
|
||||
var blum: array[50, int]
|
||||
var bc = 0
|
||||
var counts: CountTable[int]
|
||||
var n = 1
|
||||
while true:
|
||||
var p = n.firstPrimeFactor
|
||||
if (p and 3) == 3:
|
||||
let q = n div p
|
||||
if q != p and (q and 3) == 3 and q.isPrime:
|
||||
if bc < 50: blum[bc] = n
|
||||
counts.inc(n mod 10)
|
||||
inc bc
|
||||
if bc == 50:
|
||||
echo "First 50 Blum integers:"
|
||||
for i, val in blum:
|
||||
stdout.write &"{val:3}"
|
||||
stdout.write if i mod 10 == 9: '\n' else: ' '
|
||||
echo()
|
||||
elif bc == 26828 or bc mod 100000 == 0:
|
||||
echo &"The {bc:>6}th Blum integer is: {n:>7}"
|
||||
if bc == 400000:
|
||||
echo "\n% distribution of the first 400_000 Blum integers:"
|
||||
for i in [1, 3, 7, 9]:
|
||||
echo &" {counts[i]/4000:6.3f} % end in {i}"
|
||||
break
|
||||
n = if n mod 5 == 3: n + 4 else: n + 2
|
||||
43
Task/Blum-integer/Perl/blum-integer.pl
Normal file
43
Task/Blum-integer/Perl/blum-integer.pl
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use v5.36;
|
||||
use enum <false true>;
|
||||
use ntheory <is_prime gcd>;
|
||||
|
||||
sub comma { reverse ((reverse shift) =~ s/.{3}\K/,/gr) =~ s/^,//r }
|
||||
sub table ($c, @V) { my $t = $c * (my $w = 5); ( sprintf( ('%'.$w.'d')x@V, @V) ) =~ s/.{1,$t}\K/\n/gr }
|
||||
|
||||
sub is_blum ($n) {
|
||||
return false if $n < 2 or is_prime $n;
|
||||
my $factor = find_factor($n);
|
||||
my $div = int($n / $factor);
|
||||
return true if is_prime($factor) && is_prime($div) && ($div != $factor) && ($factor%4 == 3) && ($div%4 == 3);
|
||||
false;
|
||||
}
|
||||
|
||||
sub find_factor ($n, $constant = 1) {
|
||||
my($x, $rho, $factor) = (2, 1, 1);
|
||||
while ($factor == 1) {
|
||||
$rho *= 2;
|
||||
my $fixed = $x;
|
||||
for (0..$rho) {
|
||||
$x = ( $x * $x + $constant ) % $n;
|
||||
$factor = gcd(($x-$fixed), $n);
|
||||
last if 1 < $factor;
|
||||
}
|
||||
}
|
||||
$factor = find_factor($n, $constant+1) if $n == $factor;
|
||||
$factor;
|
||||
}
|
||||
|
||||
my($i, @blum, %C);
|
||||
my @nth = (26828, 1e5, 2e5, 3e5, 4e5);
|
||||
|
||||
while (++$i) {
|
||||
push @blum, $i if is_blum $i;
|
||||
last if $nth[-1] == scalar @blum;
|
||||
}
|
||||
$C{substr $_, -1, 1}++ for @blum;
|
||||
|
||||
say "The first fifty Blum integers:\n" . table 10, @blum[0..49];
|
||||
printf "The %7sth Blum integer: %9s\n", comma($_), comma $blum[$_-1] for @nth;
|
||||
say '';
|
||||
printf "$_: %6d (%.3f%%)\n", $C{$_}, 100*$C{$_}/scalar @blum for <1 3 7 9>;
|
||||
59
Task/Blum-integer/Phix/blum-integer.phix
Normal file
59
Task/Blum-integer/Phix/blum-integer.phix
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">LIMIT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e7</span><span style="color: #0000FF;">,</span> <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: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">LIMIT</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">Sieve4n_3_Primes</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sieve</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</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;">p4n3</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">*</span><span style="color: #000000;">4</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">p4n3</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">n</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">N</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000080;font-style:italic;">// collect the rest</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">p4n3</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">exit</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">n</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">by</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">p4n3</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p4n3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Sieve4n_3_Primes</span><span style="color: #0000FF;">(),</span>
|
||||
<span style="color: #000000;">BlumField</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">LIMIT</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">Blum50</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span> <span style="color: #008080;">in</span> <span style="color: #000000;">p4n3</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">bj</span> <span style="color: #008080;">in</span> <span style="color: #000000;">p4n3</span> <span style="color: #008080;">from</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">bj</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #000000;">LIMIT</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">BlumField</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span> <span style="color: #008080;">in</span> <span style="color: #000000;">BlumField</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><</span><span style="color: #000000;">50</span> <span style="color: #008080;">then</span> <span style="color: #000000;">Blum50</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">n</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">counts</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;">10</span><span style="color: #0000FF;">)]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">50</span> <span style="color: #008080;">then</span>
|
||||
<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 50 Blum integers:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Blum50</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">26828</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e5</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<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 %,7d%s Blum integer is: %,9d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4e5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<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;">"\nPercentage distribution of the first 400,000 Blum integers:\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span> <span style="color: #008080;">in</span> <span style="color: #000000;">counts</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span> <span style="color: #008080;">then</span>
|
||||
<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;">" %6.3f%% end in %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">4000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
34
Task/Blum-integer/Python/blum-integer.py
Normal file
34
Task/Blum-integer/Python/blum-integer.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
''' python example for task rosettacode.org/wiki/Blum_integer '''
|
||||
|
||||
from sympy import factorint
|
||||
|
||||
|
||||
def generate_blum():
|
||||
''' Generate the Blum integers in series '''
|
||||
for candidate in range(1, 10_000_000_000):
|
||||
fexp = factorint(candidate).items()
|
||||
if len(fexp) == 2 and sum(p[1] == 1 and p[0] % 4 == 3 for p in fexp) == 2:
|
||||
yield candidate
|
||||
|
||||
|
||||
print('First 50 Blum integers:')
|
||||
lastdigitsums = [0, 0, 0, 0]
|
||||
|
||||
for idx, blum in enumerate(generate_blum()):
|
||||
if idx < 50:
|
||||
print(f'{blum: 4}', end='\n' if (idx + 1) % 10 == 0 else '')
|
||||
elif idx + 1 in [26_828, 100_000, 200_000, 300_000, 400_000]:
|
||||
print(f'\nThe {idx+1:,}th Blum number is {blum:,}.')
|
||||
|
||||
j = blum % 10
|
||||
lastdigitsums[0] += j == 1
|
||||
lastdigitsums[1] += j == 3
|
||||
lastdigitsums[2] += j == 7
|
||||
lastdigitsums[3] += j == 9
|
||||
|
||||
if idx + 1 == 400_000:
|
||||
print('\n% distribution of the first 400,000 Blum integers is:')
|
||||
for k, dig in enumerate([1, 3, 7, 9]):
|
||||
print(f'{lastdigitsums[k]/4000:>8.5}% end in {dig}')
|
||||
|
||||
break
|
||||
37
Task/Blum-integer/Raku/blum-integer.raku
Normal file
37
Task/Blum-integer/Raku/blum-integer.raku
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use List::Divvy;
|
||||
use Lingua::EN::Numbers;
|
||||
|
||||
sub is-blum(Int $n ) {
|
||||
return False if $n.is-prime;
|
||||
my $factor = find-factor($n);
|
||||
return True if ($factor.is-prime && ( my $div = $n div $factor ).is-prime && ($div != $factor)
|
||||
&& ($factor % 4 == 3) && ($div % 4 == 3));
|
||||
False;
|
||||
}
|
||||
|
||||
sub find-factor ( Int $n, $constant = 1 ) {
|
||||
my $x = 2;
|
||||
my $rho = 1;
|
||||
my $factor = 1;
|
||||
while $factor == 1 {
|
||||
$rho *= 2;
|
||||
my $fixed = $x;
|
||||
for ^$rho {
|
||||
$x = ( $x * $x + $constant ) % $n;
|
||||
$factor = ( $x - $fixed ) gcd $n;
|
||||
last if 1 < $factor;
|
||||
}
|
||||
}
|
||||
$factor = find-factor( $n, $constant + 1 ) if $n == $factor;
|
||||
$factor;
|
||||
}
|
||||
|
||||
my @blum = lazy (2..Inf).hyper(:1000batch).grep: &is-blum;
|
||||
say "The first fifty Blum integers:\n" ~
|
||||
@blum[^50].batch(10)».fmt("%3d").join: "\n";
|
||||
say '';
|
||||
|
||||
printf "The %9s Blum integer: %9s\n", .&ordinal-digit(:c), comma @blum[$_-1] for 26828, 1e5, 2e5, 3e5, 4e5;
|
||||
|
||||
say "\nBreakdown by last digit:";
|
||||
printf "%d => %%%7.4f:\n", .key, +.value / 4e3 for sort @blum[^4e5].categorize: {.substr(*-1)}
|
||||
53
Task/Blum-integer/Wren/blum-integer.wren
Normal file
53
Task/Blum-integer/Wren/blum-integer.wren
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import "./math" for Int
|
||||
import "./fmt" for Fmt
|
||||
|
||||
var inc = [4, 2, 4, 2, 4, 6, 2, 6]
|
||||
|
||||
// Assumes n is odd.
|
||||
var firstPrimeFactor = Fn.new { |n|
|
||||
if (n == 1) return 1
|
||||
if (n%3 == 0) return 3
|
||||
if (n%5 == 0) return 5
|
||||
var k = 7
|
||||
var i = 0
|
||||
while (k * k <= n) {
|
||||
if (n%k == 0) {
|
||||
return k
|
||||
} else {
|
||||
k = k + inc[i]
|
||||
i = (i + 1) % 8
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
var blum = List.filled(50, 0)
|
||||
var bc = 0
|
||||
var counts = { 1: 0, 3: 0, 7: 0, 9: 0 }
|
||||
var i = 1
|
||||
while (true) {
|
||||
var p = firstPrimeFactor.call(i)
|
||||
if (p % 4 == 3) {
|
||||
var q = i / p
|
||||
if (q != p && q % 4 == 3 && Int.isPrime(q)) {
|
||||
if (bc < 50) blum[bc] = i
|
||||
counts[i % 10] = counts[i % 10] + 1
|
||||
bc = bc + 1
|
||||
if (bc == 50) {
|
||||
System.print("First 50 Blum integers:")
|
||||
Fmt.tprint("$3d ", blum, 10)
|
||||
System.print()
|
||||
} else if (bc == 26828 || bc % 1e5 == 0) {
|
||||
Fmt.print("The $,9r Blum integer is: $,9d", bc, i)
|
||||
if (bc == 400000) {
|
||||
System.print("\n\% distribution of the first 400,000 Blum integers:")
|
||||
for (i in [1, 3, 7, 9]) {
|
||||
Fmt.print(" $6.3f\% end in $d", counts[i]/4000, i)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
i = (i % 5 == 3) ? i + 4 : i + 2
|
||||
}
|
||||
39
Task/Blum-integer/XPL0/blum-integer-1.xpl0
Normal file
39
Task/Blum-integer/XPL0/blum-integer-1.xpl0
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
int Prime1;
|
||||
|
||||
func Semiprime(N); \Return 'true' if N is semiprime
|
||||
int N, F, C;
|
||||
[C:= 0; F:= 2;
|
||||
repeat if rem(N/F) = 0 then
|
||||
[C:= C+1;
|
||||
Prime1:= N;
|
||||
N:= N/F;
|
||||
]
|
||||
else F:= F+1;
|
||||
until F > N;
|
||||
return C = 2;
|
||||
];
|
||||
|
||||
int N, C, Prime2;
|
||||
[Format(4,0);
|
||||
N:= 3; C:= 0;
|
||||
loop [if Semiprime(N) then
|
||||
[if rem(Prime1/4) = 3 then
|
||||
[Prime2:= N/Prime1;
|
||||
if Prime2 # Prime1 and rem(Prime2/4) = 3 then
|
||||
[C:= C+1;
|
||||
if C <= 50 then
|
||||
[RlOut(0, float(N));
|
||||
if rem(C/10) = 0 then CrLf(0);
|
||||
];
|
||||
if rem(C/1000)=0 then ChOut(0, ^.);
|
||||
if C >= 26828 then
|
||||
[Text(0, "^m^jThe 26828th Blum integer is: ");
|
||||
IntOut(0, N); CrLf(0);
|
||||
quit;
|
||||
];
|
||||
];
|
||||
];
|
||||
];
|
||||
N:= N+2;
|
||||
];
|
||||
]
|
||||
52
Task/Blum-integer/XPL0/blum-integer-2.xpl0
Normal file
52
Task/Blum-integer/XPL0/blum-integer-2.xpl0
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
include xpllib; \for Print
|
||||
|
||||
int Prime1;
|
||||
|
||||
func Semiprime(N); \Returns 'true' if odd N >= 3 is semiprime
|
||||
int N, D, C;
|
||||
[C:= 0; D:= 3;
|
||||
while D*D <= N do
|
||||
[while rem(N/D) = 0 do
|
||||
[if C = 2 then return false;
|
||||
C:= C+1;
|
||||
N:= N/D;
|
||||
];
|
||||
D:= D+2;
|
||||
];
|
||||
Prime1:= N;
|
||||
return C = 1;
|
||||
];
|
||||
|
||||
int N, C, I, Goal, Prime2;
|
||||
int FD, DC(10); \final digit and digit counters
|
||||
[Text(0, "First 50 Blum integers:^m^j");
|
||||
N:= 3; C:= 0; Goal:= 100_000;
|
||||
for I:= 0 to 9 do DC(I):= 0;
|
||||
loop [if Semiprime(N) then
|
||||
[if rem(Prime1/4) = 3 then
|
||||
[Prime2:= N/Prime1;
|
||||
if rem(Prime2/4) = 3 and Prime2 # Prime1 then
|
||||
[C:= C+1;
|
||||
if C <= 50 then
|
||||
[Print("%5d", N);
|
||||
if rem(C/10) = 0 then Print("\n");
|
||||
];
|
||||
if C = 26_828 then
|
||||
Print("\nThe 26,828th Blum integer is: %7,d\n", N);
|
||||
if C = Goal then
|
||||
[Print("The %6,dth Blum integer is: %7,d\n", Goal, N);
|
||||
if Goal = 400_000 then quit;
|
||||
Goal:= Goal + 100_000;
|
||||
];
|
||||
FD:= rem(N/10);
|
||||
DC(FD):= DC(FD)+1;
|
||||
];
|
||||
];
|
||||
];
|
||||
N:= N+2;
|
||||
];
|
||||
Print("\n% distribution of the first 400,000 Blum integers:\n");
|
||||
for I:= 0 to 9 do
|
||||
if DC(I) > 0 then
|
||||
Print("%4.3f\% end in %d\n", float(DC(I)) / 4_000., I);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue