2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,3 +1,12 @@
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]).
Semiprime numbers are natural numbers that are products of exactly two (possibly equal) [[prime_number|prime numbers]].
;Example:
<big> 1679 = 23 &times; 73 </big>
(This particular number was chosen as the length of the [http://en.wikipedia.org/wiki/Arecibo_message Arecibo message]).
;Task;
Write a function determining whether a given number is semiprime.
<br><br>

View file

@ -0,0 +1,38 @@
# returns TRUE if n is semi-prime, FALSE otherwise #
# n is semi prime if it has exactly two prime factors #
PROC is semiprime = ( INT n )BOOL:
BEGIN
# We only need to consider factors between 2 and #
# sqrt( n ) inclusive. If there is only one of these #
# then it must be a prime factor and so the number #
# is semi prime #
INT factor count := 0;
FOR factor FROM 2 TO ENTIER sqrt( ABS n )
WHILE IF n MOD factor = 0 THEN
factor count +:= 1;
# check the factor isn't a repeated factor #
IF n /= factor * factor THEN
# the factor isn't the square root #
INT other factor = n OVER factor;
IF other factor MOD factor = 0 THEN
# have a repeated factor #
factor count +:= 1
FI
FI
FI;
factor count < 2
DO SKIP OD;
factor count = 1
END # is semiprime # ;
# determine the first few semi primes #
print( ( "semi primes below 100: " ) );
FOR i TO 99 DO
IF is semi prime( i ) THEN print( ( whole( i, 0 ), " " ) ) FI
OD;
print( ( newline ) );
print( ( "semi primes below between 1670 and 1690: " ) );
FOR i FROM 1670 TO 1690 DO
IF is semi prime( i ) THEN print( ( whole( i, 0 ), " " ) ) FI
OD;
print( ( newline ) )

View file

@ -0,0 +1,14 @@
defmodule Prime do
def semiprime?(n), do: length(decomposition(n)) == 2
def decomposition(n), do: decomposition(n, 2, [])
defp decomposition(n, k, acc) when n < k*k, do: Enum.reverse(acc, [n])
defp decomposition(n, k, acc) when rem(n, k) == 0, do: decomposition(div(n, k), k, [k | acc])
defp decomposition(n, k, acc), do: decomposition(n, k+1, acc)
end
IO.inspect Enum.filter(1..100, &Prime.semiprime?(&1))
Enum.each(1675..1680, fn n ->
:io.format "~w -> ~w\t~s~n", [n, Prime.semiprime?(n), Prime.decomposition(n)|>Enum.join(" x ")]
end)

View file

@ -0,0 +1,16 @@
function semiprime (n)
local divisor, count = 2, 0
while count < 3 and n ~= 1 do
if n % divisor == 0 then
n = n / divisor
count = count + 1
else
divisor = divisor + 1
end
end
return count == 2
end
for n = 1675, 1680 do
print(n, semiprime(n))
end

View file

@ -1,10 +1,10 @@
SemiPrimes := proc( n )
local fact;
fact := numtheory:-divisors( n ) minus {1, n};
fact := NumberTheory:-Divisors( n ) minus {1, n};
if numelems( fact ) in {1,2} and not( member( 'false', isprime ~ ( fact ) ) ) then
return n;
else
return NULL;
end if;
end proc:
{ seq( SemiPrime( i ), i = 1..100 ) };
{ seq( SemiPrimes( i ), i = 1..100 ) };

View file

@ -0,0 +1,26 @@
function isPrime ($n) {
if ($n -le 1) {$false}
elseif (($n -eq 2) -or ($n -eq 3)) {$true}
else{
$m = [Math]::Floor([Math]::Sqrt($n))
(@(2..$m | where {($_ -lt $n) -and ($n % $_ -eq 0) }).Count -eq 0)
}
}
function semiprime ($n) {
if($n -gt 3) {
$lim = [Math]::Floor($n/2)+1
$i = 2
while(($i -lt $lim) -and ($n%$i -ne 0)){ $i += 1}
if($i -eq $lim){@()}
elseif(-not (isPrime ($n/$i))){@()}
else{@($i,($n/$i))}
} else {@()}
}
$OFS = " x "
"1679: $(semiprime 1679)"
"87: $(semiprime 87)"
"25: $(semiprime 25)"
"12: $(semiprime 12)"
"6: $(semiprime 6)"
$OFS = " "
"semiprime form 1 to 100: $(1..100 | where {semiprime $_})"

View file

@ -1,31 +1,31 @@
/*REXX program determines if any number (or a range) is/are semiprime. */
parse arg bot top . /*obtain optional numbers from the C.L.*/
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. */
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."
/*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. */
w=max(length(bot), length(top)) /*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."
end /*n*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
isPrime: procedure; parse arg x; if x<2 then return 0 /*number too low*/
if wordpos(x, '2 3 5 7 11 13 17 19 23')\==0 then return 1 /*it's low prime*/
if x//2==0 then return 0; if x//3==0 then return 0 /*÷ by 2;÷ by 3?*/
do j=5 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*/
return 1 /*indicate that X is a prime number. */
/*────────────────────────────────────────────────────────────────────────────*/
isSemiPrime: procedure; parse arg x; if \datatype(x,'W') | x<4 then return 0
x=x/1
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; if j*j>x then return 0 /* > √ 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*/ /* [↑] never ÷ by J if J is mult. of 3*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isPrime: procedure; parse arg x; if x<2 then return 0 /*number too low?*/
if wordpos(x, '2 3 5 7 11 13 17 19 23')\==0 then return 1 /*it's low prime.*/
if x//2==0 then return 0; if x//3==0 then return 0 /*÷ by 2; ÷ by 3?*/
do j=5 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*/
return 1 /*indicate that X is a prime number. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isSemiPrime: procedure; 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; if j*j>x then return 0 /* > √ 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.*/