Semiprime: Difference between revisions

10,191 bytes added ,  1 month ago
m
 
(18 intermediate revisions by 10 users not shown)
Line 255:
semi primes below 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
semi primes below between 1670 and 1690: 1671 1673 1678 1679 1681 1685 1687 1689
</pre>
 
=={{header|ALGOL W}}==
{{Trans|C++}}
<syntaxhighlight lang="algolw">
begin % find some semi-primes - numbers with exactly 2 prime factors %
logical procedure isSemiPrime( integer value v ) ;
begin
integer a, b, c;
a := 2; b := 0; c := v;
while b < 3 and c > 1 do begin
if c rem a = 0 then begin
c := c div a;
b := b + 1
end
else a := a + 1;
end while_b_lt_3_and_c_ne_1 ;
b = 2
end isSemiPrime ;
 
for x := 2 until 99 do begin
if isSemiPrime( x ) then writeon( i_w := 1, s_w := 0, x, " " )
end for_x
end.
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
Line 431 ⟶ 459:
next i
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">100 rem Semiprime
110 cls
120 for i = 0 to 64
130 print using "## ";i semiprime$(i)
140 next i
150 end
160 sub semiprime$(n)
170 a = 2
180 c = 0
190 do while c < 3 and n > 1
200 if n mod a = 0 then n = n/a : c = c+1 else a = a+1
210 loop
220 if c = 2 then semiprime$ = "True" else semiprime$ = "False"
230 end sub</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Line 484 ⟶ 530:
170 END
</syntaxhighlight>
 
==={{header|Palo Alto Tiny BASIC}}===
{{trans|Tiny BASIC}}
<syntaxhighlight lang="basic">
10 REM SEMIPRIME
20 INPUT "ENTER AN INTEGER"N
30 LET N=ABS(N)
40 LET C=0
50 IF N<2 GOTO 90
60 FOR F=2 TO N
70 IF (N/F)*F=N LET C=C+1,N=N/F;GOTO 70
80 NEXT F
90 IF C=2 PRINT "IT IS A SEMIPRIME.";STOP
100 PRINT "IT IS NOT A SEMIPRIME.";STOP
</syntaxhighlight>
{{out}} 2 runs.
<pre>
ENTER AN INTEGER:60
IT IS NOT A SEMIPRIME.
</pre>
<pre>
ENTER AN INTEGER:33
IT IS A SEMIPRIME.
</pre>
 
==={{header|PureBasic}}===
Line 511 ⟶ 581:
CloseConsole()
End</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="vbnet">function semiprime$(n)
a = 2
c = 0
while c < 3 and n > 1
if n mod a = 0 then
n = n / a
c = c + 1
else
a = a + 1
end if
wend
if c = 2 then semiprime$ = "True" else semiprime$ = "False"
end function
 
for i = 0 to 64
print i; chr$(9); semiprime$(i)
next i</syntaxhighlight>
 
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
<syntaxhighlight lang="tinybasic"> PRINT "Enter an integer"
<syntaxhighlight lang="basic">10 REM Semiprime
INPUT N
20 PRINT "Enter an integer"
IF N < 0 THEN LET N = -N
30 INPUT N
IF N < 2 THEN GOTO 20
40 IF N < 0 THEN LET CN = 0-N
50 IF N < LET2 FTHEN =GOTO 2120
60 LET C = 0
10 IF (N/F)*F = N THEN GOTO 30
70 LET F = F + 12
80 IF (N / IFF) * F >= N THEN GOTO 20150
90 LET F = GOTOF 10+ 1
20 100 IF CF => 2N THEN PRINT "It is aGOTO semiprime."120
110 GOTO 80
IF C<> 2 THEN PRINT "It is not a semiprime."
120 IF C = 2 THEN PRINT "It is a semiprime."
END
130 IF C <> 2 THEN PRINT "It is not a semiprime."
30 LET C = C + 1
140 END
LET N = N / F
150 LET C = C + 1
GOTO 10</syntaxhighlight>
160 LET N = N / F
170 GOTO 80</syntaxhighlight>
{{out}}2 runs.
<pre>
Enter an integer
? 60
It is not a semiprime.
</pre>
<pre>
Enter an integer
? 33
It is a semiprime.
</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FUNCTION semiprime$ (n)
LET a = 2
LET c = 0
DO WHILE c < 3 AND n > 1
IF REMAINDER(n, a) = 0 THEN
LET n = n / a
LET c = c + 1
ELSE
LET a = a + 1
END IF
LOOP
IF c = 2 THEN LET semiprime$ = "True" ELSE LET semiprime$ = "False"
END FUNCTION
 
FOR i = 0 TO 64
PRINT i, semiprime$(i)
NEXT i
END</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 969 ⟶ 1,091:
$ @factor 2147483646
FACTORIZATION = "2*3*3*7*11*31*151*331"</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
{This function would normally be in a library, but it shown here for clarity}
 
procedure GetAllFactors(N: Integer;var IA: TIntegerDynArray);
{Make a list of all irreducible factor of N}
var I: integer;
begin
SetLength(IA,1);
IA[0]:=1;
for I:=2 to N do
while (N mod I)=0 do
begin
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=I;
N:=N div I;
end;
end;
 
 
function IsSemiprime(N: integer): boolean;
{Test if number is semiprime}
var IA: TIntegerDynArray;
begin
{Get all factors of N}
GetAllFactors(N,IA);
Result:=False;
{Since 1 is factor, ignore it}
if Length(IA)<>3 then exit;
Result:=IsPrime(IA[1]) and IsPrime(IA[2]);
end;
 
 
procedure Semiprimes(Memo: TMemo);
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
{Test first 100 number to see if they are semiprime}
for I:=0 to 100-1 do
if IsSemiprime(I) then
begin
Inc(Cnt);
S:=S+Format('%4d',[I]);
if (Cnt mod 10)= 0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count='+IntToStr(Cnt));
end;
 
</syntaxhighlight>
{{out}}
<pre>
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
Count=34
Elapsed Time: 5.182 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc factor num .
i = 2
while i <= sqrt num
if num mod i = 0
return i
.
i += 1
.
return 1
.
func semiprime n .
f1 = factor n
if f1 = 1
return 0
.
f2 = n div f1
if factor f1 = 1 and factor f2 = 1
return 1
.
return 0
.
for i = 1 to 1000
if semiprime i = 1
write i & " "
.
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,116 ⟶ 1,336:
</syntaxhighlight>
Output is the same of "C" version.
 
=={{header|Euler}}==
{{Trans|C++}}
<!-- syntaxhighlight lang="euler"> -->
'''begin''' '''new''' isSemiPrime; '''new''' x; '''label''' xLoop;
isSemiPrime <-
` '''formal''' v;
'''begin''' '''new''' a; '''new''' b; '''new''' c; '''label''' again;
a <- 2; b <- 0; c <- v;
again: '''if''' b < 3 '''and''' c > 1 '''then''' '''begin'''
'''if''' c '''mod''' a = 0 '''then''' '''begin'''
c <- c % a;
b <- b + 1
'''end'''
'''else''' a <- a + 1;
'''goto''' again
'''end''' '''else''' 0;
b = 2
'''end'''
'
;
x <- 1;
xLoop: '''if''' [ x <- x + 1 ] < 100 '''then''' '''begin'''
'''if''' isSemiPrime( x ) '''then''' '''out''' x '''else''' 0;
'''goto''' xLoop
'''end''' '''else''' 0
'''end''' $
<!-- </syntaxhighlight> -->
{{out}}
<pre>
NUMBER 4
NUMBER 6
NUMBER 9
NUMBER 10
...
NUMBER 91
NUMBER 93
NUMBER 94
NUMBER 95
</pre>
 
=={{header|F_Sharp|F#}}==
Line 1,320 ⟶ 1,580:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
 
<syntaxhighlight lang="jq">
# Output: a stream of proper factors (probably unsorted)
def proper_factors:
range(2; 1 + sqrt|floor) as $i
| if (. % $i) == 0
then (. / $i) as $r
| if $i == $r then $i else $i, $r end
else empty
end;
 
def is_semiprime:
.{i: as2, $n: ., nf: 0}
| until( .i > .n or .result;
| any(proper_factors;
is_prime and until(($.n /% .)i | (. =!= $n0 or is_prime) ).result;
if .nf == 2 then .result = 0
else .nf += 1
| .n /= .i
end)
| .i += 1)
| if .result == 0 then false else .nf == 2 end;
</syntaxhighlight>
'''Examples'''
Line 1,430 ⟶ 1,684:
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
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
 
{def factors
{def factors.r
{lambda {:n :i}
{if {> :i :n}
then
else {if {= {% :n :i} 0}
then :i {factors.r {/ :n :i} :i}
else {factors.r :n {+ :i 1}} }}}}
{lambda {:n}
{A.new {factors.r :n 2} }}}
-> factors
 
{factors 491} -> [491] // prime
{factors 492} -> [2,2,3,41]
{factors 493} -> [17,29] // semiprime
{factors 494} -> [2,13,19]
{factors 495} -> [3,3,5,11]
{factors 496} -> [2,2,2,2,31]
{factors 497} -> [7,71] // semiprime
{factors 498} -> [2,3,83]
{factors 499} -> [499] // prime
{factors 500} -> [2,2,5,5,5]
 
{S.replace \s by space in
{S.map {lambda {:i}
{let { {:i :i} {:f {factors :i}}
} {if {= {A.length :f} 2}
then :i={A.first :f}*{A.last :f}
else}} }
{S.serie 1 100}}}
->
4 = 2*2
6 = 2*3
9 = 3*3
10 = 2*5
14 = 2*7
15 = 3*5
21 = 3*7
22 = 2*11
25 = 5*5
26 = 2*13
33 = 3*11
34 = 2*17
35 = 5*7
38 = 2*19
39 = 3*13
46 = 2*23
49 = 7*7
51 = 3*17
55 = 5*11
57 = 3*19
58 = 2*29
62 = 2*31
65 = 5*13
69 = 3*23
74 = 2*37
77 = 7*11
82 = 2*41
85 = 5*17
86 = 2*43
87 = 3*29
91 = 7*13
93 = 3*31
94 = 2*47
95 = 5*19
</syntaxhighlight>
 
=={{header|Lingo}}==
Line 1,514 ⟶ 1,838:
<pre>{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}</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* The first part consider the cases of squares of primes, the second part the remaining cases */
semiprimep(n):=if integerp(sqrt(n)) and primep(sqrt(n)) then true else lambda([x],length(ifactors(x))=2 and unique(map(second,ifactors(x)))=[1])(n)$
 
/* Example */
sublist(makelist(i,i,100),semiprimep);
</syntaxhighlight>
{{out}}
<pre>
[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]
</pre>
 
=={{header|MiniScript}}==
Line 1,880 ⟶ 2,217:
{{out}}
<pre>(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 1678 1679)</pre>
 
=={{header|PL/0}}==
{{trans|Tiny BASIC}}
PL/0 does not handle strings. So, the program waits for entering a number, and then displays 1 if the number is a semiprime, 0 otherwise.
<syntaxhighlight lang="pascal">
var n, count, factor;
begin
? n;
if n < 0 then n := -n;
count := 0;
if n >= 2 then
begin
factor := 2;
while factor <= n do
begin
while (n / factor) * factor = n do
begin
count := count + 1; n := n / factor
end;
factor := factor + 1
end;
end;
if count = 2 then ! 1;
if count <> 2 then ! 0
end.
</syntaxhighlight>
 
=={{header|PL/I}}==
Line 1,983 ⟶ 2,346:
100 0 is NOT semiprime 2*2*25
5040 0 is NOT semiprime 2*2*1260</pre>
 
=={{header|PL/M}}==
{{Trans|C++}}
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* FIND SOME SEMI-PRIMES - NUMBERS WITH EXACTLY 2 PRIME FACTORS */
 
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* TASK */
 
/* RETURNS TRUE IF V IS SEMI-PRIME, FALSE OTHERWISE */
IS$SEMI$PRIME: PROCEDURE( V )BYTE;
DECLARE V ADDRESS;
DECLARE ( A, B, C ) ADDRESS;
A = 2; B = 0; C = V;
DO WHILE B < 3 AND C > 1;
IF C MOD A = 0 THEN DO;
C = C / A;
B = B + 1;
END;
ELSE A = A + 1;
END;
RETURN B = 2;
END IS$SEMI$PRIME;
 
DECLARE X ADDRESS;
DO X = 2 TO 99;
IF IS$SEMI$PRIME( X ) THEN DO;
CALL PR$NUMBER( X );
CALL PR$CHAR( ' ' );
END;
END;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|PowerShell}}==
Line 2,643 ⟶ 3,062:
return true
</syntaxhighlight>
 
=={{header|RPL}}==
<code>PDIV</code> is defined at [[Prime decomposition#RPL|Prime decomposition]]
≪ <span style="color:blue">'''PDIV'''</span> SIZE 2 == ≫ '<span style="color:blue">'''SPR1?'''</span>' STO
≪ { } 1 100 '''FOR''' n '''IF''' n <span style="color:blue">'''SPR1?'''</span> '''THEN''' n + '''END NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 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 }
</pre>
 
=={{header|Ruby}}==
Line 3,010 ⟶ 3,439:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var semiprime = Fn.new { |n|
if (n < 3) return false
var nf = 0
1,983

edits