Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
3
Task/Semiprime/00DESCRIPTION
Normal file
3
Task/Semiprime/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Semiprime numbers are natural numbers that are products of exactly two (possibly equal) [[prime_number|prime numbers]]. Example: 1679 = 23 × 73 (This particular number was chosen as the length of the [http://en.wikipedia.org/wiki/Arecibo_message Arecibo message]).
|
||||
|
||||
Write a function determining whether a given number is semiprime.
|
||||
2
Task/Semiprime/00META.yaml
Normal file
2
Task/Semiprime/00META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Prime Numbers
|
||||
21
Task/Semiprime/Ada/semiprime.ada
Normal file
21
Task/Semiprime/Ada/semiprime.ada
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
with Prime_Numbers, Ada.Text_IO;
|
||||
|
||||
procedure Test_Semiprime is
|
||||
|
||||
package Integer_Numbers is new
|
||||
Prime_Numbers (Natural, 0, 1, 2);
|
||||
use Integer_Numbers;
|
||||
|
||||
begin
|
||||
for N in 1 .. 100 loop
|
||||
if Decompose(N)'Length = 2 then -- N is a semiprime;
|
||||
Ada.Text_IO.Put(Integer'Image(Integer(N)));
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
for N in 1675 .. 1680 loop
|
||||
if Decompose(N)'Length = 2 then -- N is a semiprime;
|
||||
Ada.Text_IO.Put(Integer'Image(Integer(N)));
|
||||
end if;
|
||||
end loop;
|
||||
end Test_Semiprime;
|
||||
55
Task/Semiprime/AutoHotkey/semiprime.ahk
Normal file
55
Task/Semiprime/AutoHotkey/semiprime.ahk
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
SetBatchLines -1
|
||||
k := 1
|
||||
loop, 100
|
||||
{
|
||||
m := semiprime(k)
|
||||
StringSplit, m_m, m, -
|
||||
if ( m_m1 = "yes" )
|
||||
list .= k . " "
|
||||
k++
|
||||
}
|
||||
MsgBox % list
|
||||
list :=
|
||||
;===================================================================================================================================
|
||||
k := 1675
|
||||
loop, 5
|
||||
{
|
||||
m := semiprime(k)
|
||||
StringSplit, m_m, m, -
|
||||
if ( m_m1 = "yes" )
|
||||
list1 .= semiprime(k) . "`n"
|
||||
else
|
||||
list1 .= semiprime(k) . "`n"
|
||||
k++
|
||||
}
|
||||
MsgBox % list1
|
||||
list1 :=
|
||||
;===================================================================================================================================
|
||||
; The function==========================================================================================================================
|
||||
semiprime(k)
|
||||
{
|
||||
start := floor(sqrt(k))
|
||||
loop, % floor(sqrt(k)) - 1
|
||||
{
|
||||
if ( mod(k, start) = 0 )
|
||||
new .= floor(start) . "*" . floor(k//start) . ","
|
||||
start--
|
||||
}
|
||||
|
||||
StringSplit, index, new, `,
|
||||
|
||||
if ( index0 = 2 )
|
||||
{
|
||||
StringTrimRight, new, new, 1
|
||||
StringSplit, 2_ind, new, *
|
||||
if (mod(2_ind2, 2_ind1) = 0) && ( 2_ind1 != 2_ind2 )
|
||||
new := "N0- " . k . " - " . new
|
||||
else
|
||||
new := "yes- " . k . " - " . new
|
||||
}
|
||||
else
|
||||
new := "N0- " . k . " - " . new
|
||||
return new
|
||||
}
|
||||
;=================================================================================================================================================
|
||||
esc::Exitapp
|
||||
21
Task/Semiprime/C++/semiprime.cpp
Normal file
21
Task/Semiprime/C++/semiprime.cpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include <iostream>
|
||||
|
||||
bool isSemiPrime( int c )
|
||||
{
|
||||
int a = 2, b = 0;
|
||||
while( b < 3 && c != 1 )
|
||||
{
|
||||
if( !( c % a ) )
|
||||
{ c /= a; b++; }
|
||||
else a++;
|
||||
}
|
||||
return b == 2;
|
||||
}
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
for( int x = 2; x < 100; x++ )
|
||||
if( isSemiPrime( x ) )
|
||||
std::cout << x << " ";
|
||||
|
||||
return 0;
|
||||
}
|
||||
21
Task/Semiprime/C/semiprime.c
Normal file
21
Task/Semiprime/C/semiprime.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int semiprime(int n)
|
||||
{
|
||||
int p, f = 0;
|
||||
for (p = 2; f < 2 && p*p <= n; p++)
|
||||
while (0 == n % p)
|
||||
n /= p, f++;
|
||||
|
||||
return f + (n > 1) == 2;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 2; i < 100; i++)
|
||||
if (semiprime(i)) printf(" %d", i);
|
||||
putchar('\n');
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
Task/Semiprime/Common-Lisp/semiprime.lisp
Normal file
9
Task/Semiprime/Common-Lisp/semiprime.lisp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defun semiprimep (n &optional (a 2))
|
||||
(cond ((> a (isqrt n)) nil)
|
||||
((zerop (rem n a)) (and (primep a) (primep (/ n a))))
|
||||
(t (semiprimep n (+ a 1)))))
|
||||
|
||||
(defun primep (n &optional (a 2))
|
||||
(cond ((> a (isqrt n)) t)
|
||||
((zerop (rem n a)) nil)
|
||||
(t (primep n (+ a 1)))))
|
||||
19
Task/Semiprime/D/semiprime.d
Normal file
19
Task/Semiprime/D/semiprime.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
bool semiprime(long n) pure nothrow @safe @nogc {
|
||||
auto nf = 0;
|
||||
foreach (immutable i; 2 .. n + 1) {
|
||||
while (n % i == 0) {
|
||||
if (nf == 2)
|
||||
return false;
|
||||
nf++;
|
||||
n /= i;
|
||||
}
|
||||
}
|
||||
return nf == 2;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
foreach (immutable n; 1675 .. 1681)
|
||||
writeln(n, " -> ", n.semiprime);
|
||||
}
|
||||
8
Task/Semiprime/Forth/semiprime.fth
Normal file
8
Task/Semiprime/Forth/semiprime.fth
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
: semiprime?
|
||||
0 swap dup 2 do
|
||||
begin dup i mod 0= while i / swap 1+ swap repeat
|
||||
over 1 > over i dup * < or if leave then
|
||||
loop 1 > abs + 2 =
|
||||
;
|
||||
|
||||
: test 100 2 do i semiprime? if i . then loop cr ;
|
||||
23
Task/Semiprime/Go/semiprime.go
Normal file
23
Task/Semiprime/Go/semiprime.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func semiprime(n int) bool {
|
||||
nf := 0
|
||||
for i := 2; i <= n; i++ {
|
||||
for n%i == 0 {
|
||||
if nf == 2 {
|
||||
return false
|
||||
}
|
||||
nf++
|
||||
n /= i
|
||||
}
|
||||
}
|
||||
return nf == 2
|
||||
}
|
||||
|
||||
func main() {
|
||||
for v := 1675; v <= 1680; v++ {
|
||||
fmt.Println(v, "->", semiprime(v))
|
||||
}
|
||||
}
|
||||
4
Task/Semiprime/Haskell/semiprime-1.hs
Normal file
4
Task/Semiprime/Haskell/semiprime-1.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
isSemiprime :: Int -> Bool
|
||||
isSemiprime n = (length factors) == 2 && (product factors) == n ||
|
||||
(length factors) == 1 && (head factors) ^ 2 == n
|
||||
where factors = primeFactors n
|
||||
4
Task/Semiprime/Haskell/semiprime-2.hs
Normal file
4
Task/Semiprime/Haskell/semiprime-2.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
isSemiprime :: Int -> Bool
|
||||
isSemiprime n = case (primeFactors n) of
|
||||
[f1, f2] -> f1 * f2 == n
|
||||
otherwise -> False
|
||||
9
Task/Semiprime/Icon/semiprime.icon
Normal file
9
Task/Semiprime/Icon/semiprime.icon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
link "factors"
|
||||
|
||||
procedure main(A)
|
||||
every nf := semiprime(n := !A) do write(n," = ",nf[1]," * ",nf[2])
|
||||
end
|
||||
|
||||
procedure semiprime(n) # Succeeds and produces the factors only if n is semiprime.
|
||||
return (2 = *(nf := factors(n)), nf)
|
||||
end
|
||||
1
Task/Semiprime/J/semiprime-1.j
Normal file
1
Task/Semiprime/J/semiprime-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
isSemiPrime=: 2 = #@q: ::0:"0
|
||||
2
Task/Semiprime/J/semiprime-2.j
Normal file
2
Task/Semiprime/J/semiprime-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
I. isSemiPrime i.100
|
||||
4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95
|
||||
57
Task/Semiprime/Java/semiprime.java
Normal file
57
Task/Semiprime/Java/semiprime.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SemiPrime{
|
||||
private static final BigInteger TWO = BigInteger.valueOf(2);
|
||||
|
||||
public static List<BigInteger> primeDecomp(BigInteger a){
|
||||
// impossible for values lower than 2
|
||||
if(a.compareTo(TWO) < 0){
|
||||
return null;
|
||||
}
|
||||
|
||||
//quickly handle even values
|
||||
List<BigInteger> result = new ArrayList<BigInteger>();
|
||||
while(a.and(BigInteger.ONE).equals(BigInteger.ZERO)){
|
||||
a = a.shiftRight(1);
|
||||
result.add(TWO);
|
||||
}
|
||||
|
||||
//left with odd values
|
||||
if(!a.equals(BigInteger.ONE)){
|
||||
BigInteger b = BigInteger.valueOf(3);
|
||||
while(b.compareTo(a) < 0){
|
||||
if(b.isProbablePrime(10)){
|
||||
BigInteger[] dr = a.divideAndRemainder(b);
|
||||
if(dr[1].equals(BigInteger.ZERO)){
|
||||
result.add(b);
|
||||
a = dr[0];
|
||||
}
|
||||
}
|
||||
b = b.add(TWO);
|
||||
}
|
||||
result.add(b); //b will always be prime here...
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean isSemi(BigInteger x){
|
||||
List<BigInteger> decomp = primeDecomp(x);
|
||||
return decomp != null && decomp.size() == 2;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
for(int i = 2; i <= 100; i++){
|
||||
if(isSemi(BigInteger.valueOf(i))){
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
for(int i = 1675; i <= 1680; i++){
|
||||
if(isSemi(BigInteger.valueOf(i))){
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Task/Semiprime/Julia/semiprime.julia
Normal file
1
Task/Semiprime/Julia/semiprime.julia
Normal file
|
|
@ -0,0 +1 @@
|
|||
semiprime(n) = sum(values(factor(n))) == 2
|
||||
5
Task/Semiprime/Mathematica/semiprime-1.math
Normal file
5
Task/Semiprime/Mathematica/semiprime-1.math
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
semiPrimeQ[n_Integer] := Module[{factors, numfactors},
|
||||
factors = FactorInteger[n] // Transpose;
|
||||
numfactors = factors[[2]] // Total ;
|
||||
numfactors == 2
|
||||
]
|
||||
2
Task/Semiprime/Mathematica/semiprime-2.math
Normal file
2
Task/Semiprime/Mathematica/semiprime-2.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
semiPrimeQ[#] & /@ Range[100];
|
||||
Position[%, True] // Flatten
|
||||
25
Task/Semiprime/Objeck/semiprime.objeck
Normal file
25
Task/Semiprime/Objeck/semiprime.objeck
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
class SemiPrime {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
for(i := 0; i < 100; i+=1;) {
|
||||
if(SemiPrime(i)) {
|
||||
"{$i} "->Print();
|
||||
};
|
||||
};
|
||||
IO.Console->PrintLine();
|
||||
}
|
||||
|
||||
function : native : SemiPrime(n : Int) ~ Bool {
|
||||
nf := 0;
|
||||
for(i := 2; i <= n; i+=1;) {
|
||||
while(n%i = 0) {
|
||||
if(nf = 2) {
|
||||
return false;
|
||||
};
|
||||
nf+=1;
|
||||
n /= i;
|
||||
};
|
||||
};
|
||||
|
||||
return nf = 2;
|
||||
}
|
||||
}
|
||||
1
Task/Semiprime/PARI-GP/semiprime-1.pari
Normal file
1
Task/Semiprime/PARI-GP/semiprime-1.pari
Normal file
|
|
@ -0,0 +1 @@
|
|||
issemi(n)=bigomega(n)==2
|
||||
5
Task/Semiprime/PARI-GP/semiprime-2.pari
Normal file
5
Task/Semiprime/PARI-GP/semiprime-2.pari
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
issemi(n)={
|
||||
forprime(p=2,97,if(n%p==0, return(isprime(n/p))));
|
||||
if(isprime(n), return(0));
|
||||
bigomega(n)==2
|
||||
};
|
||||
77
Task/Semiprime/PARI-GP/semiprime-3.pari
Normal file
77
Task/Semiprime/PARI-GP/semiprime-3.pari
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
long
|
||||
issemiprime(GEN n)
|
||||
{
|
||||
if (typ(n) != t_INT)
|
||||
pari_err_TYPE("issemiprime", n);
|
||||
if (signe(n) <= 0)
|
||||
return 0;
|
||||
|
||||
ulong nn = itou_or_0(n);
|
||||
if (nn)
|
||||
return uissemiprime(nn);
|
||||
|
||||
pari_sp ltop = avma;
|
||||
if (!mpodd(n)) {
|
||||
long ret = mod4(n) && isprime(shifti(n, -1));
|
||||
avma = ltop;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
long p;
|
||||
forprime_t primepointer;
|
||||
u_forprime_init(&primepointer, 3, 997);
|
||||
while ((p = u_forprime_next(&primepointer))) {
|
||||
if (dvdis(n, p)) {
|
||||
long ret = isprime(diviuexact(n, p));
|
||||
avma = ltop;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (isprime(n))
|
||||
return 0;
|
||||
|
||||
if (DEBUGLEVEL > 3)
|
||||
pari_printf("issemi: Number is a composite with no small prime factors; using general factoring mechanisms.");
|
||||
|
||||
GEN fac = Z_factor_until(n, shifti(n, -1)); /* Find a nontrivial factor -- returns just the factored part */
|
||||
GEN expo = gel(fac, 2);
|
||||
GEN pr = gel(fac, 1);
|
||||
long len = glength(expo);
|
||||
if (len > 2) {
|
||||
avma = ltop;
|
||||
return 0;
|
||||
}
|
||||
if (len == 2) {
|
||||
if (cmpis(gel(expo, 1), 1) > 0 || cmpis(gel(expo, 2), 1) > 0) {
|
||||
avma = ltop;
|
||||
return 0;
|
||||
}
|
||||
GEN P = gel(pr, 1);
|
||||
GEN Q = gel(pr, 2);
|
||||
long ret = isprime(P) && isprime(Q) && equalii(mulii(P, Q), n);
|
||||
avma = ltop;
|
||||
return ret;
|
||||
}
|
||||
if (len == 1) {
|
||||
long e = itos(gel(expo, 1));
|
||||
if (e == 2) {
|
||||
GEN P = gel(pr, 1);
|
||||
long ret = isprime(P) && equalii(sqri(P), n);
|
||||
avma = ltop;
|
||||
return ret;
|
||||
} else if (e > 2) {
|
||||
avma = ltop;
|
||||
return 0;
|
||||
}
|
||||
GEN P = gel(pr, 1);
|
||||
long ret = isprime(P) && isprime(diviiexact(n, P));
|
||||
avma = ltop;
|
||||
return ret;
|
||||
}
|
||||
|
||||
pari_err_BUG(pari_sprintf("Z_factor_until returned an unexpected value %Ps at n = %Ps, exiting...", fac, n));
|
||||
avma = ltop;
|
||||
return 0; /* never used */
|
||||
}
|
||||
89
Task/Semiprime/PL-I/semiprime.pli
Normal file
89
Task/Semiprime/PL-I/semiprime.pli
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
*process source attributes xref nest or(!);
|
||||
/*--------------------------------------------------------------------
|
||||
* 22.02.2014 Walter Pachl using the is_prime code from
|
||||
* PL/I 'prime decomposition'
|
||||
* 23.02. WP start test for second prime with 2 or first prime found
|
||||
*-------------------------------------------------------------------*/
|
||||
spb: Proc options(main);
|
||||
Dcl a(10) Bin Fixed(31)
|
||||
Init(900660121,2,4,1679,1234567,32768,99,9876543,100,5040);
|
||||
Dcl (x,n,nf,i,j) Bin Fixed(31) Init(0);
|
||||
Dcl f(3) Bin Fixed(31);
|
||||
Dcl txt Char(30) Var;
|
||||
Dcl bit Bit(1);
|
||||
Do i=1 To hbound(a);
|
||||
bit=is_semiprime(a(i));
|
||||
Select(nf);
|
||||
When(0,1) txt=' is prime';
|
||||
When(2) txt=' is semiprime '!!factors(a(i));
|
||||
Otherwise txt=' is NOT semiprime '!!factors(a(i));
|
||||
End;
|
||||
Put Edit(a(i),bit,txt)(Skip,f(10),x(1),b(1),a);
|
||||
End;
|
||||
|
||||
is_semiprime: Proc(x) Returns(bit(1));
|
||||
/*--------------------------------------------------------------------
|
||||
* Returns '1'b if x is semiprime, '0'b otherwise
|
||||
* in addition
|
||||
* it sets f(1) and f(2) to the first (or only) prime factor(s)
|
||||
*-------------------------------------------------------------------*/
|
||||
Dcl x Bin Fixed(31);
|
||||
nf=0;
|
||||
f=0;
|
||||
x=a(i);
|
||||
n=x;
|
||||
f(1)=2;
|
||||
loop:
|
||||
Do While(nf<=2 & n>1);
|
||||
If is_prime(n) Then Do;
|
||||
Call mem(n);
|
||||
Leave loop;
|
||||
End;
|
||||
Else Do;
|
||||
loop2:
|
||||
Do j=f(1) By 1 While(j*j<=n);
|
||||
If is_prime(j)&mod(n,j)=0 Then Do;
|
||||
Call mem(j);
|
||||
n=n/j;
|
||||
Leave loop2;
|
||||
End;
|
||||
End;
|
||||
End;
|
||||
End;
|
||||
Return(nf=2);
|
||||
End;
|
||||
|
||||
is_prime: Proc(n) Returns(bit(1));
|
||||
Dcl n Bin Fixed(31);
|
||||
Dcl i Bin Fixed(31);
|
||||
If n < 2 Then Return('0'b);
|
||||
If n = 2 Then Return('1'b);
|
||||
If mod(n,2)=0 Then Return('0'b);
|
||||
Do i = 3 by 2 While(i*i<=n);
|
||||
If mod(n,i)=0 Then Return('0'b);
|
||||
End;
|
||||
Return('1'b);
|
||||
End is_prime;
|
||||
|
||||
mem: Proc(x);
|
||||
Dcl x Bin Fixed(31);
|
||||
nf+=1;
|
||||
f(nf)=x;
|
||||
End;
|
||||
|
||||
factors: Proc(x) Returns(Char(150) Var);
|
||||
Dcl x Bin Fixed(31);
|
||||
Dcl (res,net) Char(150) Var Init('');
|
||||
Dcl (i,f3) Bin Fixed(31);
|
||||
res=f(1)!!'*'!!f(2);
|
||||
f3=x/(f(1)*f(2));
|
||||
If f3>1 Then
|
||||
res=res!!'*'!!f3;
|
||||
Do i=1 To length(res);
|
||||
If substr(res,i,1)>' ' Then
|
||||
net=net!!substr(res,i,1);
|
||||
End;
|
||||
Return(net);
|
||||
End;
|
||||
|
||||
End spb;
|
||||
41
Task/Semiprime/Pascal/semiprime.pascal
Normal file
41
Task/Semiprime/Pascal/semiprime.pascal
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
program SemiPrime;
|
||||
{$IFDEF FPC}
|
||||
{$Mode objfpc}// compiler switch to use result
|
||||
{$ELSE}
|
||||
{$APPTYPE CONSOLE} // for Delphi
|
||||
{$ENDIF}
|
||||
uses
|
||||
primTrial;
|
||||
|
||||
function isSemiprime(n: longWord;doWrite:boolean): boolean;
|
||||
var
|
||||
fac1 : LongWord;
|
||||
begin
|
||||
//a simple isAlmostPrime(n,2) would do without output;
|
||||
fac1 := SmallFactor(n);
|
||||
IF fac1 < n then
|
||||
Begin
|
||||
n := n div fac1;
|
||||
result := SmallFactor(n) = n;
|
||||
if result AND doWrite then
|
||||
write(fac1:10,'*',n:11)
|
||||
end
|
||||
else
|
||||
result := false;
|
||||
end;
|
||||
var
|
||||
i,k : longWord;
|
||||
BEGIN
|
||||
For i := 2 to 97 do
|
||||
IF isSemiPrime(i,false) then
|
||||
write(i:3);
|
||||
writeln;
|
||||
//test for big numbers
|
||||
k := 4000*1000*1000;
|
||||
i := k-100;
|
||||
repeat
|
||||
IF isSemiPrime(i,true) then
|
||||
writeln(' = ',i:10);
|
||||
inc(i);
|
||||
until i> k;
|
||||
END.
|
||||
15
Task/Semiprime/Perl-6/semiprime.pl6
Normal file
15
Task/Semiprime/Perl-6/semiprime.pl6
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
sub is-semiprime (Int $n --> Bool) {
|
||||
not $n.is-prime and
|
||||
.is-prime given
|
||||
$n div first $n %% *,
|
||||
grep &is-prime, 2 .. *;
|
||||
}
|
||||
|
||||
use Test;
|
||||
my @primes = grep &is-prime, 2 .. 100;
|
||||
for ^5 {
|
||||
nok is-semiprime([*] my @f1 = @primes.roll(1)), ~@f1;
|
||||
ok is-semiprime([*] my @f2 = @primes.roll(2)), ~@f2;
|
||||
nok is-semiprime([*] my @f3 = @primes.roll(3)), ~@f3;
|
||||
nok is-semiprime([*] my @f4 = @primes.roll(4)), ~@f4;
|
||||
}
|
||||
4
Task/Semiprime/Perl/semiprime-1.pl
Normal file
4
Task/Semiprime/Perl/semiprime-1.pl
Normal file
|
|
@ -0,0 +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";
|
||||
9
Task/Semiprime/Perl/semiprime-2.pl
Normal file
9
Task/Semiprime/Perl/semiprime-2.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);
|
||||
}
|
||||
21
Task/Semiprime/PicoLisp/semiprime.l
Normal file
21
Task/Semiprime/PicoLisp/semiprime.l
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(de factor (N)
|
||||
(make
|
||||
(let
|
||||
(D 2
|
||||
L (1 2 2 . (4 2 4 2 4 6 2 6 .))
|
||||
M (sqrt N) )
|
||||
(while (>= M D)
|
||||
(if (=0 (% N D))
|
||||
(setq M
|
||||
(sqrt (setq N (/ N (link D)))) )
|
||||
(inc 'D (pop 'L)) ) )
|
||||
(link N) ) ) )
|
||||
|
||||
(println
|
||||
(filter
|
||||
'((X)
|
||||
(let L (factor X)
|
||||
(and (cdr L) (not (cddr L))) ) )
|
||||
(conc (range 1 100) (range 1675 1680)) ) )
|
||||
|
||||
(bye)
|
||||
8
Task/Semiprime/Python/semiprime-1.py
Normal file
8
Task/Semiprime/Python/semiprime-1.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
from prime_decomposition import decompose
|
||||
|
||||
def semiprime(n):
|
||||
d = decompose(n)
|
||||
try:
|
||||
return next(d) * next(d) == n
|
||||
except:
|
||||
return False
|
||||
5
Task/Semiprime/Python/semiprime-2.py
Normal file
5
Task/Semiprime/Python/semiprime-2.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
>>> semiprime(1679)
|
||||
True
|
||||
>>> [n for n in range(1,101) if semiprime(n)]
|
||||
[4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95]
|
||||
>>>
|
||||
1
Task/Semiprime/README
Normal file
1
Task/Semiprime/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/Semiprime
|
||||
56
Task/Semiprime/REXX/semiprime-1.rexx
Normal file
56
Task/Semiprime/REXX/semiprime-1.rexx
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* REXX ---------------------------------------------------------------
|
||||
* 20.02.2014 Walter Pachl relying on 'prime decomposition'
|
||||
* 21.02.2014 WP Clarification: I copied the algorithm created by
|
||||
* Gerard Schildberger under the task referred to above
|
||||
* 21.02.2014 WP Make sure that factr is not called illegally
|
||||
*--------------------------------------------------------------------*/
|
||||
Call test 4
|
||||
Call test 9
|
||||
Call test 10
|
||||
Call test 12
|
||||
Call test 1679
|
||||
Exit
|
||||
|
||||
test:
|
||||
Parse Arg z
|
||||
If is_semiprime(z) Then Say z 'is semiprime' fl
|
||||
Else Say z 'is NOT semiprime' fl
|
||||
Return
|
||||
|
||||
is_semiprime:
|
||||
Parse Arg z
|
||||
If z<1 | datatype(z,'W')=0 Then Do
|
||||
Say 'Argument ('z') must be a natural number (1, 2, 3, ...)'
|
||||
fl=''
|
||||
End
|
||||
Else
|
||||
fl=factr(z)
|
||||
Return words(fl)=2
|
||||
|
||||
/*----------------------------------FACTR subroutine-----------------*/
|
||||
factr: procedure; parse arg x 1 z,list /*sets X&Z to arg1, LIST=''. */
|
||||
if x==1 then return '' /*handle the special case of X=1.*/
|
||||
j=2; call .factr /*factor for the only even prime.*/
|
||||
j=3; call .factr /*factor for the 1st odd prime.*/
|
||||
j=5; call .factr /*factor for the 2nd odd prime.*/
|
||||
j=7; call .factr /*factor for the 3rd odd prime.*/
|
||||
j=11; call .factr /*factor for the 4th odd prime.*/
|
||||
j=13; call .factr /*factor for the 5th odd prime.*/
|
||||
j=17; call .factr /*factor for the 6th odd prime.*/
|
||||
/* [?] could be optimized more.*/
|
||||
/* [?] J in loop starts at 17+2*/
|
||||
do y=0 by 2; j=j+2+y//4 /*insure J isn't divisible by 3. */
|
||||
if right(j,1)==5 then iterate /*fast check for divisible by 5. */
|
||||
if j*j>z then leave /*are we higher than the v of Z ?*/
|
||||
if j>Z then leave /*are we higher than value of Z ?*/
|
||||
call .factr /*invoke .FACTR for some factors.*/
|
||||
end /*y*/ /* [?] only tests up to the v X.*/
|
||||
/* [?] LIST has a leading blank.*/
|
||||
if z==1 then return list /*if residual=unity, don't append*/
|
||||
return list z /*return list, append residual. */
|
||||
/*-------------------------------.FACTR internal subroutine----------*/
|
||||
.factr: do while z//j==0 /*keep dividing until we can't. */
|
||||
list=list j /*add number to the list (J). */
|
||||
z=z%j /*% (percent) is integer divide.*/
|
||||
end /*while z··· */ /* // ?---remainder integer ÷.*/
|
||||
return /*finished, now return to invoker*/
|
||||
30
Task/Semiprime/REXX/semiprime-2.rexx
Normal file
30
Task/Semiprime/REXX/semiprime-2.rexx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*REXX program determines if any number (or a range) is/are semiprime.*/
|
||||
parse arg bot top . /*obtain #s from the command line*/
|
||||
if bot=='' then bot=random() /*so, the user wants us to guess.*/
|
||||
if top=='' then top=bot /*maybe define a range of numbers*/
|
||||
w=max(length(bot), length(top)) /*get maximum width of numbers. */
|
||||
if w>digits() then numeric digits w /*is there enough digits ? */
|
||||
do n=bot to top /*show results for a range of #s.*/
|
||||
if isSemiPrime(n) then say right(n,w) ' is semiprime.'
|
||||
else say right(n,w) " isn't semiprime."
|
||||
end /*n*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────ISPRIME subroutine──────────────────*/
|
||||
isPrime: procedure; parse arg x; if x<2 then return 0
|
||||
if wordpos(x,'2 3 5 7')\==0 then return 1 /*handle some special cases*/
|
||||
do i=2 for 2; if x//i==0 then return 0; end /*i*/ /*÷ by 2 & 3*/
|
||||
do j=5 by 6 until j*j>x; if x//j==0 then return 0 /*¬ a prime#*/
|
||||
if x//(j+2)==0 then return 0 /*¬ a prime#*/
|
||||
end /*j*/
|
||||
return 1 /*X is a prime number, for sure.*/
|
||||
/*──────────────────────────────────ISSEMIPRIME subroutine──────────────*/
|
||||
isSemiPrime: procedure; arg x; if \datatype(x,'W') | x<4 then return 0
|
||||
x=x/1 /*normalize the X number. */
|
||||
do i=2 for 2; if x//i==0 then if isPrime(x%i) then return 1
|
||||
else return 0
|
||||
end /*i*/ /* [↑] divides by two and three.*/
|
||||
do j=5 by 6; if j*j>x then return 0 /*÷ by #s. */
|
||||
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*/ /*[↑] never ÷ by # divisible by 3*/
|
||||
15
Task/Semiprime/Racket/semiprime-1.rkt
Normal file
15
Task/Semiprime/Racket/semiprime-1.rkt
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
|
||||
(define (pair-factorize n)
|
||||
"Return all two-number factorizations of a number"
|
||||
(let ([up-limit (integer-sqrt n)])
|
||||
(map (λ (x) (list x (/ n x)))
|
||||
(filter (λ (x) (<= x up-limit)) (divisors n)))))
|
||||
|
||||
(define (semiprime n)
|
||||
"Determine if a number is semiprime i.e. a product of two primes.
|
||||
Check if any pair of complete factors consists of primes."
|
||||
(for/or ((pair (pair-factorize n)))
|
||||
(for/and ((el pair))
|
||||
(prime? el))))
|
||||
12
Task/Semiprime/Racket/semiprime-2.rkt
Normal file
12
Task/Semiprime/Racket/semiprime-2.rkt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
|
||||
(define (semiprime n)
|
||||
"Alternative implementation.
|
||||
Check if there are two prime factors whose product is the argument
|
||||
or if there is a single prime factor whose square is the argument"
|
||||
(let ([prime-factors (factorize n)])
|
||||
(or (and (= (length prime-factors) 1)
|
||||
(= (expt (caar prime-factors) (cadar prime-factors)) n))
|
||||
(and (= (length prime-factors) 2)
|
||||
(= (foldl (λ (x y) (* (car x) y)) 1 prime-factors) n)))))
|
||||
12
Task/Semiprime/Ruby/semiprime.rb
Normal file
12
Task/Semiprime/Ruby/semiprime.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
require 'prime'
|
||||
# 75.prime_division # Returns the factorization.75 divides by 3 once and by 5 twice => [[3, 1], [5, 2]]
|
||||
|
||||
class Integer
|
||||
def semi_prime?
|
||||
prime_division.map( &:last ).inject( &:+ ) == 2
|
||||
end
|
||||
end
|
||||
|
||||
p 1679.semi_prime? # true
|
||||
p ( 1..100 ).select( &:semi_prime? )
|
||||
# [4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95]
|
||||
20
Task/Semiprime/Scala/semiprime.scala
Normal file
20
Task/Semiprime/Scala/semiprime.scala
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
object Semiprime extends App {
|
||||
|
||||
def isSP(n: Int): Boolean = {
|
||||
var nf: Int = 0
|
||||
var l = n
|
||||
for (i <- 2 to l/2) {
|
||||
while (l % i == 0) {
|
||||
if (nf == 2) return false
|
||||
nf +=1
|
||||
l /= i
|
||||
}
|
||||
}
|
||||
nf == 2
|
||||
}
|
||||
|
||||
(2 to 100) filter {isSP(_) == true} foreach {i => print("%d ".format(i))}
|
||||
println
|
||||
1675 to 1681 foreach {i => println(i+" -> "+isSP(i))}
|
||||
|
||||
}
|
||||
27
Task/Semiprime/Seed7/semiprime.seed7
Normal file
27
Task/Semiprime/Seed7/semiprime.seed7
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func boolean: semiPrime (in var integer: n) is func
|
||||
result
|
||||
var boolean: isSemiPrime is TRUE;
|
||||
local
|
||||
var integer: p is 2;
|
||||
var integer: f is 0;
|
||||
begin
|
||||
while f < 2 and p**2 <= n do
|
||||
while n rem p = 0 do
|
||||
n := n div p;
|
||||
incr(f);
|
||||
end while;
|
||||
incr(p);
|
||||
end while;
|
||||
isSemiPrime := f + ord(n > 1) = 2;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: v is 0;
|
||||
begin
|
||||
for v range 1675 to 1680 do
|
||||
writeln(v <& " -> " <& semiPrime(v));
|
||||
end for;
|
||||
end func;
|
||||
24
Task/Semiprime/Tcl/semiprime.tcl
Normal file
24
Task/Semiprime/Tcl/semiprime.tcl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package require math::numtheory
|
||||
|
||||
proc isSemiprime n {
|
||||
if {!($n & 1)} {
|
||||
return [::math::numtheory::isprime [expr {$n >> 1}]]
|
||||
}
|
||||
for {set i 3} {$i*$i < $n} {incr i 2} {
|
||||
if {$n / $i * $i != $n && [::math::numtheory::isprime $i]} {
|
||||
if {[::math::numtheory::isprime [expr {$n/$i}]]} {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
for {set n 1675} {$n <= 1680} {incr n} {
|
||||
puts -nonewline "$n is ... "
|
||||
if {[isSemiprime $n]} {
|
||||
puts "a semiprime"
|
||||
} else {
|
||||
puts "NOT a semiprime"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue