September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,6 +1,15 @@
|
|||
Semiprime numbers are natural numbers that are products of exactly two (possibly equal) [[prime_number|prime numbers]].
|
||||
|
||||
|
||||
'''Semiprimes''' are also known as:
|
||||
:::* '''semi-primes'''
|
||||
:::* '''biprimes'''
|
||||
:::* '''bi-primes'''
|
||||
:::* ''' ''2-almost'' ''' primes
|
||||
:::* or simply: ''' ''P<sub>2</sub> '' '''
|
||||
|
||||
|
||||
|
||||
;Example:
|
||||
<big> 1679 = 23 × 73 </big>
|
||||
|
||||
|
|
@ -9,4 +18,10 @@ Semiprime numbers are natural numbers that are products of exactly two (possibly
|
|||
|
||||
;Task;
|
||||
Write a function determining whether a given number is semiprime.
|
||||
|
||||
|
||||
;See also:
|
||||
* The Wikipedia article: [http://mathworld.wolfram.com/Semiprime.html semiprime].
|
||||
* The Wikipedia article: [http://mathworld.wolfram.com/AlmostPrime.html almost prime].
|
||||
* The OEIS article: [http://oeis.org/A001358 semiprimes] which has a shorter definition: ''the product of two primes''.
|
||||
<br><br>
|
||||
|
|
|
|||
71
Task/Semiprime/360-Assembly/semiprime.360
Normal file
71
Task/Semiprime/360-Assembly/semiprime.360
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
* Semiprime 14/03/2017
|
||||
SEMIPRIM CSECT
|
||||
USING SEMIPRIM,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) save previous context
|
||||
ST R13,4(R15) link backward
|
||||
ST R15,8(R13) link forward
|
||||
LR R13,R15 set addressability
|
||||
LA R10,PG pgi=0
|
||||
LA R8,0 m=0
|
||||
L R6,=F'2' i=2
|
||||
DO WHILE=(C,R6,LE,=F'100') do i=2 to 100
|
||||
ST R6,N n=i
|
||||
LA R9,0 f=0
|
||||
LA R7,2 j=2
|
||||
LOOPJ EQU * do j=2 while f<2 and j*j<=n
|
||||
C R9,=F'2' if f<2
|
||||
BNL EXITJ then exit do j
|
||||
LR R5,R7 j
|
||||
MR R4,R7 *j
|
||||
C R5,N if j*j<=n
|
||||
BH EXITJ then exit do j
|
||||
LOOPK EQU * do while n mod j=0
|
||||
L R4,N n
|
||||
SRDA R4,32 ~
|
||||
DR R4,R7 /j
|
||||
LTR R4,R4 if n mod <>0
|
||||
BNZ EXITK then exit do j
|
||||
ST R5,N n=n/j
|
||||
LA R9,1(R9) f=f+1
|
||||
B LOOPK enddo k
|
||||
EXITK LA R7,1(R7) j++
|
||||
B LOOPJ enddo j
|
||||
EXITJ L R4,N n
|
||||
IF C,R4,GT,=F'1' THEN if n>1 then
|
||||
LA R2,1 g=1
|
||||
ELSE , else
|
||||
LA R2,0 g=0
|
||||
ENDIF , endif
|
||||
AR R2,R9 +f
|
||||
IF C,R2,EQ,=F'2' THEN if f+(n>1)=2 then
|
||||
XDECO R6,XDEC edit i
|
||||
MVC 0(5,R10),XDEC+7 output i
|
||||
LA R10,5(R10) pgi=pgi+10
|
||||
LA R8,1(R8) m=m+1
|
||||
LR R4,R8 m
|
||||
SRDA R4,32 ~
|
||||
D R4,=F'16' m/16
|
||||
IF LTR,R4,Z,R4 THEN if m mod 16=0 then
|
||||
XPRNT PG,L'PG print buffer
|
||||
MVC PG,=CL80' ' clear buffer
|
||||
LA R10,PG pgi=0
|
||||
ENDIF , endif
|
||||
ENDIF , endif
|
||||
LA R6,1(R6) i++
|
||||
ENDDO , enddo i
|
||||
XPRNT PG,L'PG print buffer
|
||||
MVC PG,=CL80'..... semiprimes' init buffer
|
||||
XDECO R8,XDEC edit m
|
||||
MVC PG(5),XDEC+7 output m
|
||||
XPRNT PG,L'PG print buffer
|
||||
L R13,4(0,R13) restore previous savearea pointer
|
||||
LM R14,R12,12(R13) restore previous context
|
||||
XR R15,R15 rc=0
|
||||
BR R14 exit
|
||||
N DS F n
|
||||
PG DC CL80' ' buffer
|
||||
XDEC DS CL12 temp
|
||||
YREGS
|
||||
END SEMIPRIM
|
||||
14
Task/Semiprime/Clojure/semiprime.clj
Normal file
14
Task/Semiprime/Clojure/semiprime.clj
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(ns example
|
||||
(:gen-class))
|
||||
|
||||
(defn semi-prime? [n]
|
||||
(loop [a 2
|
||||
b 0
|
||||
c n]
|
||||
(cond
|
||||
(> b 2) false
|
||||
(<= c 1) (= b 2)
|
||||
(= 0 (rem c a)) (recur a (inc b) (int (/ c a)))
|
||||
:else (recur (inc a) b c))))
|
||||
|
||||
(println (filter semi-prime? (range 1 100)))
|
||||
18
Task/Semiprime/Kotlin/semiprime.kotlin
Normal file
18
Task/Semiprime/Kotlin/semiprime.kotlin
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// version 1.1.2
|
||||
|
||||
fun isSemiPrime(n: Int): Boolean {
|
||||
var nf = 0
|
||||
var nn = n
|
||||
for (i in 2..nn)
|
||||
while (nn % i == 0) {
|
||||
if (nf == 2) return false
|
||||
nf++
|
||||
nn /= i
|
||||
}
|
||||
return nf == 2
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
for (v in 1675..1680)
|
||||
println("$v ${if (isSemiPrime(v)) "is" else "isn't"} semi-prime")
|
||||
}
|
||||
18
Task/Semiprime/Nim/semiprime.nim
Normal file
18
Task/Semiprime/Nim/semiprime.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
proc isSemiPrime(k: int): string =
|
||||
var
|
||||
i: int = 2
|
||||
compte: int = 0
|
||||
x: int = k
|
||||
while i<=x and compte<3:
|
||||
if (x mod i)==0:
|
||||
x = x div i
|
||||
compte += 1
|
||||
else:
|
||||
i += 1
|
||||
if compte==2:
|
||||
result = "is semi-prime"
|
||||
else:
|
||||
result = "isn't semi-prime"
|
||||
|
||||
for k in 1675..1680:
|
||||
echo k," ",isSemiPrime(k)
|
||||
38
Task/Semiprime/Perl-6/semiprime-2.pl6
Normal file
38
Task/Semiprime/Perl-6/semiprime-2.pl6
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
sub is-semiprime ( Int $n where * > 0 ) {
|
||||
return False if $n.is-prime;
|
||||
my $factor = find-factor( $n );
|
||||
return True if $factor.is-prime && ( $n div $factor ).is-prime;
|
||||
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;
|
||||
}
|
||||
|
||||
INIT my $start = now;
|
||||
|
||||
# Infinite list of semiprimes
|
||||
constant @semiprimes = 4, 6, 9, -> $p { ($p + 1 ... &is-semiprime).tail } ... *;
|
||||
|
||||
# Show the semiprimes < 100
|
||||
say 'Semiprimes less than 100:';
|
||||
say @semiprimes[^ @semiprimes.first: * > 100, :k ], "\n";
|
||||
|
||||
# Check individual integers, or in this case, a range
|
||||
my $s = 2⁹⁷ - 1;
|
||||
say "Is $_ semiprime?: ", is-semiprime( $_ ) for $s .. $s + 30;
|
||||
|
||||
say 'elapsed seconds: ', now - $start;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use ntheory "factor";
|
||||
print join(" ", grep { scalar factor($_) == 2 } 1..100),"\n";
|
||||
print join(" ", grep { scalar factor($_) == 2 } 1675..1681),"\n";
|
||||
print join(" ", grep { scalar factor($_) == 2 } (2,4,99,100,1679,5040,32768,1234567,9876543,900660121)),"\n";
|
||||
use ntheory "is_semiprime";
|
||||
for ([1..100], [1675..1681], [2,4,99,100,1679,5030,32768,1234567,9876543,900660121]) {
|
||||
print join(" ",grep { is_semiprime($_) } @$_),"\n";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,2 @@
|
|||
use ntheory qw/factor is_prime trial_factor/;
|
||||
sub issemi {
|
||||
my $n = shift;
|
||||
if ((my @p = trial_factor($n,500)) > 1) {
|
||||
return 0 if @p > 2;
|
||||
return !!is_prime($p[1]) if @p == 2;
|
||||
}
|
||||
2 == factor($n);
|
||||
}
|
||||
use ntheory "factor";
|
||||
print join(" ", grep { scalar factor($_) == 2 } 1..100),"\n";
|
||||
|
|
|
|||
9
Task/Semiprime/Perl/semiprime-3.pl
Normal file
9
Task/Semiprime/Perl/semiprime-3.pl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use ntheory qw/factor is_prime trial_factor/;
|
||||
sub issemi {
|
||||
my $n = shift;
|
||||
if ((my @p = trial_factor($n,500)) > 1) {
|
||||
return 0 if @p > 2;
|
||||
return !!is_prime($p[1]) if @p == 2;
|
||||
}
|
||||
2 == factor($n);
|
||||
}
|
||||
18
Task/Semiprime/Phix/semiprime.phix
Normal file
18
Task/Semiprime/Phix/semiprime.phix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
function semiprime(integer n)
|
||||
sequence f = prime_factors(n)
|
||||
integer l = length(f)
|
||||
return (l=2 and n=f[1]*f[2]) or (l=1 and n=power(f[1],2))
|
||||
end function
|
||||
|
||||
procedure test(integer start, integer stop)
|
||||
sequence s = {}
|
||||
for i=start to stop do
|
||||
if semiprime(i) then
|
||||
s &= i
|
||||
end if
|
||||
end for
|
||||
?s
|
||||
?length(s)
|
||||
end procedure
|
||||
test(1,100)
|
||||
test(1675,1680)
|
||||
|
|
@ -2,12 +2,16 @@
|
|||
parse arg bot top . /*obtain optional arguments from the CL*/
|
||||
if bot=='' | bot=="," then bot=random() /*None given? User wants us to guess.*/
|
||||
if top=='' | top=="," then top=bot /*maybe define a range of numbers. */
|
||||
w=max(length(bot), length(top)) /*obtain the maximum width of numbers. */
|
||||
tell= top=>0 | top==bot /*should results be shown to the term? */
|
||||
w=max(length(bot), length(top)) + 5 /*obtain the maximum width of numbers. */
|
||||
numeric digits max(9, w) /*ensure there're enough decimal digits*/
|
||||
do n=bot to top /*show results for a range of numbers. */
|
||||
if isSemiPrime(n) then say right(n, w) " is semiprime."
|
||||
else say right(n, w) " isn't semiprime."
|
||||
#=0 /*initialize number of semiprimes found*/
|
||||
do n=bot to abs(top) /*show results for a range of numbers. */
|
||||
?=isSemiPrime(n); #=#+? /*Is N a semiprime?; Maybe bump counter*/
|
||||
if tell then say right(n,w) right(word("isn't" 'is', ?+1), 6) 'semiprime.'
|
||||
end /*n*/
|
||||
say
|
||||
if bot\==top then say 'found ' # " semiprimes."
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
isPrime: procedure; parse arg x; if x<2 then return 0 /*number too low?*/
|
||||
|
|
|
|||
41
Task/Semiprime/REXX/semiprime-3.rexx
Normal file
41
Task/Semiprime/REXX/semiprime-3.rexx
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*REXX program determines if any integer (or a range of integers) is/are semiprime. */
|
||||
parse arg bot top . /*obtain optional arguments from the CL*/
|
||||
if bot=='' | bot=="," then bot=random() /*None given? User wants us to guess.*/
|
||||
if top=='' | top=="," then top=bot /*maybe define a range of numbers. */
|
||||
tell= bot=>0 & top=>0 /*should results be shown to the term? */
|
||||
w=max(length(bot), length(top)) /*obtain the maximum width of numbers. */
|
||||
!.=; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1; !.19=1; !.23=1; !.29=1; !.31=1
|
||||
numeric digits max(9, w) /*ensure there're enough decimal digits*/
|
||||
#=0 /*initialize number of semiprimes found*/
|
||||
do n=abs(bot) to abs(top) /*show results for a range of numbers. */
|
||||
?=isSemiPrime(n); #=#+? /*Is N a semiprime?; Maybe bump counter*/
|
||||
if tell then say right(n,w) right(word("isn't" 'is', ?+1), 6) 'semiprime.'
|
||||
end /*n*/
|
||||
say
|
||||
if bot\==top then say 'found ' # " semiprimes."
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
isPrime: procedure expose !.; parse arg x; if x<2 then return 0 /*number too low?*/
|
||||
if !.x==1 then return 1 /*a known prime. */
|
||||
if x// 2==0 then return 0; if x//3==0 then return 0 /*÷ by 2;÷by 3?*/
|
||||
parse var x '' -1 _; if _==5 then return 0 /*last digit a 5?*/
|
||||
if x// 7==0 then return 0; if x//11==0 then return 0 /*÷ by 7;÷by 11?*/
|
||||
if x//13==0 then return 0; if x//17==0 then return 0 /*÷ by 13;÷by 17?*/
|
||||
if x//19==0 then return 0; if x//23==0 then return 0 /*÷ by 19;÷by 23?*/
|
||||
do j=29 by 6 until j*j>x; if x//j==0 then return 0 /*not a prime. */
|
||||
if x//(j+2)==0 then return 0 /* " " " */
|
||||
end /*j*/
|
||||
!.x=1; return 1 /*indicate that X is a prime number. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
isSemiPrime: procedure expose !.; parse arg x; if x<4 then return 0
|
||||
|
||||
do i=2 for 2; if x//i==0 then if isPrime(x%i) then return 1
|
||||
else return 0
|
||||
end /*i*/
|
||||
/* ___ */
|
||||
do j=5 by 6 until j*j>x /* > √ x ?*/
|
||||
do k=j by 2 for 2; if x//k==0 then if isPrime(x%k) then return 1
|
||||
else return 0
|
||||
end /*k*/ /* [↑] see if 2nd factor is prime or ¬*/
|
||||
end /*j*/ /* [↑] J is never a multiple of three.*/
|
||||
return 0
|
||||
8
Task/Semiprime/Zkl/semiprime.zkl
Normal file
8
Task/Semiprime/Zkl/semiprime.zkl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fcn semiprime(n){
|
||||
reg f = 0;
|
||||
p:=2; while(f < 2 and p*p <= n){
|
||||
while(0 == n % p){ n /= p; f+=1; }
|
||||
p+=1;
|
||||
}
|
||||
return(f + (n > 1) == 2);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue