Semiprime: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|PHP}}: Added.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 32:
{{trans|C++}}
<
V a = 2
V b = 0
Line 43:
R b == 2
print((1..100).filter(n -> is_semiprime(n)))</
{{out}}
Line 52:
=={{header|360 Assembly}}==
{{trans|C}}
<
SEMIPRIM CSECT
USING SEMIPRIM,R13 base register
Line 122:
XDEC DS CL12 temp
YREGS
END SEMIPRIM</
{{out}}
<pre>
Line 132:
=={{header|Action!}}==
<
INT a,b
Line 159:
FI
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Semiprime.png Screenshot from Atari 8-bit computer]
Line 175:
This imports the package '''Prime_Numbers''' from [[Prime decomposition#Ada]].
<
procedure Test_Semiprime is
Line 195:
end if;
end loop;
end Test_Semiprime;</
It outputs all semiprimes below 100 and all semiprimes between 1675 and 1680:
Line 212:
=={{header|ALGOL 68}}==
<
# n is semi prime if it has exactly two prime factors #
PROC is semiprime = ( INT n )BOOL:
Line 250:
OD;
print( ( newline ) )
</syntaxhighlight>
{{out}}
<pre>
Line 259:
=={{header|Arturo}}==
<
2 = size factors.prime x
]
print select 1..100 => semiPrime?</
{{out}}
Line 271:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
<
k := 1
loop, 100
Line 325:
}
;=================================================================================================================================================
esc::Exitapp</
{{output}}
<Pre>
Line 336:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SEMIPRIME.AWK
BEGIN {
Line 365:
return(nf == 2)
}
</syntaxhighlight>
{{out}}
<pre>
Line 376:
==={{header|ASIC}}===
{{trans|Tiny BASIC}}
<
REM Semiprime
PRINT "Enter an integer ";
Line 400:
ENDIF
END
</syntaxhighlight>
{{out}}
<pre>
Line 412:
==={{header|BASIC256}}===
<
a = 2
c = 0
Line 430:
print i, semiprime$(i)
next i
end</
==={{header|FreeBASIC}}===
<
dim as uinteger a = 2, c = 0
while c < 3 andalso n > 1
Line 449:
for i as uinteger = 0 to 64
print i, semiprime(i)
next i</
==={{header|GW-BASIC}}===
<
20 N=ABS(N)
30 C = 0
Line 459:
60 IF N MOD F = 0 THEN C = C + 1 : N = N / F ELSE F = F + 1
70 IF N > 1 THEN GOTO 60
80 IF C=2 THEN PRINT "It's a semiprime." ELSE PRINT "It is not a semiprime."</
==={{header|Minimal BASIC}}===
Line 465:
{{works with|Commodore BASIC|3.5}}
{{works with|Nascom ROM BASIC|4.7}}
<
10 REM Semiprime
20 PRINT "Enter an integer";
Line 483:
160 PRINT "It is not a semiprime."
170 END
</syntaxhighlight>
==={{header|PureBasic}}===
<
a.i = 2
c.i = 0
Line 510:
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()
End</
==={{header|Tiny BASIC}}===
<
INPUT N
IF N < 0 THEN LET N = -N
Line 528:
30 LET C = C + 1
LET N = N / F
GOTO 10</
==={{header|Yabasic}}===
<
a = 2
c = 0
Line 549:
print i, chr$(9), semiprime$(i)
next i
end</
=={{header|Bracmat}}==
When Bracmat is asked to take the square (or any other) root of a number, it does so by first finding the number's prime factors. It can do that for numbers up to 2^32 or 2^64 (depending on compiler and processor).
<
m n a b
. 2^-64:?m
Line 559:
& !arg^!m
: (#%?a^!m*#%?b^!m|#%?a^!n&!a:?b)
& (!a.!b);</
Test with numbers < 2^63:
<
& whl
' ( -1+!u:>2:?u
Line 568:
|
)
);</
Output:
Line 619:
=={{header|C}}==
<
int semiprime(int n)
Line 639:
return 0;
}</
{{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|C sharp|C#}}==
<
static void Main(string[] args)
{
Line 673:
return b == 2;
}
</syntaxhighlight>
{{out}}
<pre>
Line 729:
=={{header|C++}}==
<
#include <iostream>
Line 751:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 759:
=={{header|Clojure}}==
{{trans|C}}
<
(ns example
(:gen-class))
Line 774:
(println (filter semi-prime? (range 1 100)))
</syntaxhighlight>
{{Out}}
<pre>
Line 781:
=={{header|Common Lisp}}==
<
(cond ((> a (isqrt n)) nil)
((zerop (rem n a)) (and (primep a) (primep (/ n a))))
Line 789:
(cond ((> a (isqrt n)) t)
((zerop (rem n a)) nil)
(t (primep n (+ a 1)))))</
Example Usage:
Line 800:
=={{header|Crystal}}==
{{trans|D}}
<
nf = 0
(2..n).each do |i|
Line 812:
end
(1675..1681).each { |n| puts "#{n} -> #{semiprime(n)}" }</
{{out}}
<pre>1675 -> false
Line 823:
Faster version using 'factor' function from [U|Li]nux Core Utilities library.
<
`factor #{n}`.split(' ').size == 3
end
n = 0xffffffffffffffff_u64 # 2**64 - 1 = 18446744073709551615
(n-50..n).each { |n| puts "#{n} -> #{semiprime(n)}" }</
{{out}}
<pre>18446744073709551565 -> false
Line 883:
=={{header|D}}==
{{trans|Go}}
<
auto nf = 0;
foreach (immutable i; 2 .. n + 1) {
Line 901:
foreach (immutable n; 1675 .. 1681)
writeln(n, " -> ", n.semiprime);
}</
{{out}}
<pre>1675 -> false
Line 912:
=={{header|DCL}}==
Given a file primes.txt is the list of primes up to the sqrt(2^31-1), i.e. 46337;
<
$ if p1 .lt. 2
$ then
Line 959:
$
$ clean:
$ close primes</
{{out}}
<pre>$ @factor 6
Line 971:
=={{header|EchoLisp}}==
<
(lib 'math)
(define (semi-prime? n)
Line 992:
(prime-factors 100000000042)
→ (2 50000000021)
</syntaxhighlight>
=={{header|Elixir}}==
<
def semiprime?(n), do: length(decomposition(n)) == 2
Line 1,008:
Enum.each(1675..1680, fn n ->
:io.format "~w -> ~w\t~s~n", [n, Prime.semiprime?(n), Prime.decomposition(n)|>Enum.join(" x ")]
end)</
{{out}}
Line 1,025:
Another using prime factors from [[Prime_decomposition#Erlang]] :
<
-module(factors).
-export([factors/1,kthfactor/2]).
Line 1,045:
_ ->
false end.
</syntaxhighlight>
{out}
<pre>
Line 1,087:
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM SEMIPRIME_NUMBER
Line 1,114:
PRINT
END PROGRAM
</syntaxhighlight>
Output is the same of "C" version.
=={{header|F_Sharp|F#}}==
<
let rec loop currentN candidateFactor numberOfFactors =
if numberOfFactors > 2 then numberOfFactors
Line 1,132:
|> Seq.choose (fun n -> if isSemiprime n then Some(n) else None)
|> Seq.toList
|> printfn "%A"</
{{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]
Line 1,140:
=={{header|Factor}}==
{{works with|Factor|0.98}}
<syntaxhighlight lang="text">USING: io kernel math.primes.factors prettyprint sequences ;
: semiprime? ( n -- ? ) factors length 2 = ;</
Displaying the semiprimes under 100:
<syntaxhighlight lang="text">100 <iota> [ semiprime? ] filter [ bl ] [ pprint ] interleave nl</
{{out}}
<pre>
Line 1,153:
=={{header|Forth}}==
<
0 swap dup 2 do
begin dup i mod 0= while i / swap 1+ swap repeat
Line 1,160:
;
: test 100 2 do i semiprime? if i . then loop cr ;</
{{out}}
<pre>
Line 1,168:
=={{header|Frink}}==
<
{
factors = factor[n]
Line 1,176:
return sum == 2
}</
=={{header|Go}}==
<
import "fmt"
Line 1,201:
fmt.Println(v, "->", semiprime(v))
}
}</
{{out}}
<pre>
Line 1,214:
=={{header|Haskell}}==
{{libheader|Data.Numbers.Primes}}
<
isSemiprime n = (length factors) == 2 && (product factors) == n ||
(length factors) == 1 && (head factors) ^ 2 == n
where factors = primeFactors n</
Alternative (and faster) implementation using pattern matching:
<
isSemiprime n = case (primeFactors n) of
[f1, f2] -> f1 * f2 == n
otherwise -> False</
=={{header|Icon}} and {{header|Unicon}}==
Works in both languages:
<
procedure main(A)
Line 1,236:
procedure semiprime(n) # Succeeds and produces the factors only if n is semiprime.
return (2 = *(nf := factors(n)), nf)
end</
{{Out}}
Line 1,250:
Implementation:
<
Example use: find all semiprimes less than 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</
Description: factor the number and count the primes in the factorization, is it 2?
Line 1,264:
Like the Ada example here, this borrows from [[Prime decomposition#Java|Prime decomposition]] and shows the semiprimes below 100 and from 1675 to 1680.
<
import java.util.ArrayList;
import java.util.List;
Line 1,320:
}
}
}</
{{out}}
<pre>4 6 9 10 14 15 21 22 25 26 27 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 81 82 85 86 87 91 93 94 95
Line 1,331:
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:
Line 1,345:
| any(proper_factors;
is_prime and (($n / .) | (. == $n or is_prime) );
</syntaxhighlight>
'''Examples'''
<syntaxhighlight lang="jq">
(1679, 1680) | "\(.) => \(is_semiprime)"
</syntaxhighlight>
{{out}}
<pre>
Line 1,359:
{{works with|Julia|0.6}}
<
issemiprime(n::Integer) = sum(values(factor(n))) == 2
@show filter(issemiprime, 1:100)</
{{out}}
Line 1,368:
=={{header|Kotlin}}==
{{trans|Go}}
<
fun isSemiPrime(n: Int): Boolean {
Line 1,385:
for (v in 1675..1680)
println("$v ${if (isSemiPrime(v)) "is" else "isn't"} semi-prime")
}</
{{out}}
Line 1,398:
=={{header|Ksh}}==
<
#!/bin/ksh
Line 1,434:
done
echo
</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
Line 1,440:
=={{header|Lingo}}==
<
div = 2
cnt = 0
Line 1,452:
end repeat
return cnt=2
end</
<
repeat with i = 1 to 100
if isSemiPrime(i) then res.add(i)
end repeat
put res</
{{out}}
Line 1,466:
=={{header|Lua}}==
<syntaxhighlight lang="lua">
function semiprime (n)
local divisor, count = 2, 0
Line 1,483:
print(n, semiprime(n))
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,495:
=={{header|Maple}}==
<
local fact;
fact := NumberTheory:-Divisors( n ) minus {1, n};
Line 1,504:
end if;
end proc:
{ seq( SemiPrimes( i ), i = 1..100 ) };</
Output:
<syntaxhighlight lang="maple">
{ 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 }
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
factors = FactorInteger[n] // Transpose;
numfactors = factors[[2]] // Total ;
numfactors == 2
]</
Example use: find all semiprimes less than 100:
<
Position[%, True] // Flatten</
{{out}}
<pre>{4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51,
Line 1,524:
=={{header|MiniScript}}==
<
divisor = 2
primes = 0
Line 1,543:
if isSemiprime(i) then results.push i
end for
print results</
{{output}}
Line 1,551:
=={{header|NewLisp}}==
<syntaxhighlight lang="newlisp">
;;; Practically identical to the EchoLisp solution
(define (semiprime? n)
Line 1,561:
(while (not (semiprime? x)) (-- x))
(println "Biggest semiprime reachable: " x " = " ((factor x) 0) " x " ((factor x) 1))
</syntaxhighlight>
{{output}}
<pre>
Line 1,569:
=={{header|Nim}}==
<
var
i = 2
Line 1,583:
for k in 1675..1680:
echo k, (if k.isSemiPrime(): " is" else: " isn’t"), " semi-prime"</
{{output}}
Line 1,596:
=={{header|Objeck}}==
{{trans|Go}}
<
class SemiPrime {
function : Main(args : String[]) ~ Nil {
Line 1,621:
return nf = 2;
}
}</
Output:
Line 1,628:
=={{header|Oforth}}==
<
| i |
0 2 n sqrt asInteger for: i [ while(n i /mod swap 0 &=) [ ->n 1+ ] drop ]
n 1 > ifTrue: [ 1+ ] 2 == ; </
{{out}}
Line 1,640:
=={{header|PARI/GP}}==
<
A faster version might use trial division and primality testing:
<
forprime(p=2,97,if(n%p==0, return(isprime(n/p))));
if(isprime(n), return(0));
bigomega(n)==2
};</
To get faster, partial factorization can be used. At this time GP does not have access to meaningful partial factorization (though it can get it to some extent through flags on <code>factorint</code>), so this version is in PARI:
<
issemiprime(GEN n)
{
Line 1,726:
avma = ltop;
return 0; /* never used */
}</
=={{header|Pascal}}==
{{libheader|primTrial}}{{works with|Free Pascal}}
<
{$IFDEF FPC}
{$Mode objfpc}// compiler switch to use result
Line 1,771:
inc(i);
until i> k;
END.</
;output:
<pre>
Line 1,792:
{{libheader|ntheory}}
With late versions of the ntheory module, we can use <tt>is_semiprime</tt> to get answers for 64-bit numbers in single microseconds.
<
for ([1..100], [1675..1681], [2,4,99,100,1679,5030,32768,1234567,9876543,900660121]) {
print join(" ",grep { is_semiprime($_) } @$_),"\n";
}</
{{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
Line 1,802:
One can also use <tt>factor</tt> in scalar context, which gives the number of factors (like <tt>bigomega</tt> in Pari/GP and <tt>PrimeOmega</tt> in Mathematica). This skips some optimizations but at these small sizes it doesn't matter.
<
print join(" ", grep { scalar factor($_) == 2 } 1..100),"\n";</
While <tt>is_semiprime</tt> is the fastest way, we can do some of its pre-tests by hand, such as:
<
sub issemi {
my $n = shift;
Line 1,814:
}
2 == factor($n);
}</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">semiprime</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">))==</span><span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)&</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1680</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1675</span><span style="color: #0000FF;">),</span><span style="color: #000000;">semiprime</span><span style="color: #0000FF;">),{</span><span style="color: #004600;">pp_IntCh</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,831:
=={{header|PHP}}==
{{trans|TypeScript}}
<
<?php
// Semiprime
Line 1,853:
"It is a semiprime.\n" : "It is not a semiprime.\n");
?>
</syntaxhighlight>
{{out}}
<pre>
Line 1,865:
=={{header|PicoLisp}}==
<
(make
(let
Line 1,885:
(conc (range 1 100) (range 1675 1680)) ) )
(bye)</
{{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/I}}==
<
/*--------------------------------------------------------------------
* 22.02.2014 Walter Pachl using the is_prime code from
Line 1,979:
End spb;
</syntaxhighlight>
'''Output:'''
<pre> 900660121 1 is semiprime 30011*30011
Line 1,993:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function isPrime ($n) {
if ($n -le 1) {$false}
Line 2,020:
$OFS = " "
"semiprime form 1 to 100: $(1..100 | where {semiprime $_})"
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,033:
=={{header|Python}}==
This imports [[Prime decomposition#Python]]
<
def semiprime(n):
Line 2,040:
return next(d) * next(d) == n
except StopIteration:
return False</
{{out}}
From Idle:
<
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]
>>> </
=={{header|Quackery}}==
Line 2,054:
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
<
say "Semiprimes less than 100:" cr
100 times [ i^ semiprime if [ i^ echo sp ] ]</
{{out}}
Line 2,066:
=={{header|Racket}}==
The first implementation considers all pairs of factors multiplying up to the given number and determines if any of them is a pair of primes.
<
(require math)
Line 2,080:
(for/or ((pair (pair-factorize n)))
(for/and ((el pair))
(prime? el))))</
The alternative implementation operates directly on the list of prime factors and their multiplicities. It is approximately 1.6 times faster than the first one (according to some simple tests of mine).
<
(require math)
Line 2,094:
(= (expt (caar prime-factors) (cadar prime-factors)) n))
(and (= (length prime-factors) 2)
(= (foldl (λ (x y) (* (car x) y)) 1 prime-factors) n)))))</
=={{header|Raku}}==
(formerly Perl 6)
Here is a naive, grossly inefficient implementation.
<syntaxhighlight lang="raku"
not $n.is-prime and
.is-prime given
Line 2,112:
nok is-semiprime([*] my @f3 = @primes.roll(3)), ~@f3;
nok is-semiprime([*] my @f4 = @primes.roll(4)), ~@f4;
}</
{{out}}
<pre>ok 1 - 17
Line 2,139:
{{works with|Rakudo|2017.02}}
<syntaxhighlight lang="raku"
return False if $n.is-prime;
my $factor = find-factor( $n );
Line 2,177:
say 'elapsed seconds: ', now - $start;
</syntaxhighlight>
{{out}}
<pre>Semiprimes less than 100:
Line 2,217:
=={{header|REXX}}==
===version 1===
<
* 20.02.2014 Walter Pachl relying on 'prime decomposition'
* 21.02.2014 WP Clarification: I copied the algorithm created by
Line 2,272:
z=z%j /*% (percent) is integer divide.*/
end /*while z··· */ /* // ?---remainder integer ÷.*/
return /*finished, now return to invoker*/</
'''Output'''
<pre>4 is semiprime 2 2
Line 2,286:
The '''isPrime''' function could be optimized by utilizing an integer square root function instead of testing if '''j*j>x''' for every divisor.
<
parse arg bot top . /*obtain optional arguments from the CL*/
if bot=='' | bot=="," then bot=random() /*None given? User wants us to guess.*/
Line 2,320:
else return 0
end /*k*/ /* [↑] see if 2nd factor is prime or ¬*/
end /*j*/ /* [↑] J is never a multiple of three.*/</
{{out|output|text= when using the input of: <tt> -1 106 </tt>}}
Line 2,496:
It gets its speed increase by the use of memoization of the prime numbers found, an unrolled primality (division) check, and other speed improvements.
<
parse arg bot top . /*obtain optional arguments from the CL*/
if bot=='' | bot=="," then bot=random() /*None given? User wants us to guess.*/
Line 2,536:
end /*k*/ /* [↑] see if 2nd factor is prime or ¬*/
end /*j*/ /* [↑] J is never a multiple of three.*/
return 0</
{{out|output|text= is identical to the previous REXX version.}} <br><br>
=={{header|Ring}}==
<
prime = 1679
decomp(prime)
Line 2,565:
next
return true
</syntaxhighlight>
=={{header|Ruby}}==
<
# 75.prime_division # Returns the factorization.75 divides by 3 once and by 5 twice => [[3, 1], [5, 2]]
Line 2,580:
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]
</syntaxhighlight>
Faster version using 'factor' function from [U|Li]nux Core Utilities library.
<
`factor #{n}`.split(' ').size == 3
end
n = 2**72 - 1 #4722366482869645213695
(n-50..n).each { |n| puts "#{n} -> #{semiprime(n)}" }</
{{out}}
<pre>4722366482869645213645 -> false
Line 2,642:
=={{header|Rust}}==
<syntaxhighlight lang="text">extern crate primal;
fn isqrt(n: usize) -> usize {
Line 2,697:
fn test6() {
assert_eq!((2..1_000_000).filter(|&n| is_semiprime(n)).count(), 210_035);
}</
functional version of is_semiprime:
<
fn iter(x: usize, start: usize, acc: &[usize]) -> Vec<usize> {
if acc.len() > 2 {return acc.to_vec()} // break for semi_prime
Line 2,710:
}
iter(n, 2, &[]).len() == 2
}</
{{out}}
<pre>
Line 2,726:
=={{header|Scala}}==
{{works with|Scala 2.9.1}}
<
def isSP(n: Int): Boolean = {
Line 2,745:
1675 to 1681 foreach {i => println(i+" -> "+isSP(i))}
}</
{{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
Line 2,757:
=={{header|Seed7}}==
<
const func boolean: semiPrime (in var integer: n) is func
Line 2,783:
writeln(v <& " -> " <& semiPrime(v));
end for;
end func;</
{{out}}
Line 2,797:
=={{header|Sidef}}==
Built-in:
<
say is_semiprime(2**256 - 1) #=> false</
User-defined function, with trial division up to a given bound '''B''':
<
with (n.trial_factor(B)) { |f|
Line 2,811:
}
say [2,4,99,100,1679,32768,1234567,9876543,900660121].grep(is_semiprime)</
{{out}}
<pre>
Line 2,819:
=={{header|Swift}}==
<
func primes(n: Int) -> AnyGenerator<Int> {
Line 2,855:
}
return false
}</
=={{header|Tcl}}==
{{tcllib|math::numtheory}}
<
proc isSemiprime n {
Line 2,882:
puts "NOT a semiprime"
}
}</
{{out}}
<pre>
Line 2,895:
== {{header|TypeScript}} ==
{{trans|ASIC}}
<
// Semiprime
Line 2,920:
readline.close();
});
</syntaxhighlight>
{{out}}
<pre>
Line 2,933:
=={{header|Wren}}==
{{trans|Go}}
<
if (n < 3) return false
var nf = 0
Line 2,948:
for (v in 1675..1680) {
System.print("%(v) -> %(semiprime.call(v) ? "is" : "is not") semi-prime")
}</
{{out}}
Line 2,961:
=={{header|XPL0}}==
<
int N, F, C;
[C:= 0; F:= 2;
Line 2,977:
if Semiprime(N) then
[IntOut(0, N); ChOut(0, ^ )];
]</
{{out}}
Line 2,986:
=={{header|zkl}}==
{{trans|C}}
<
reg f = 0;
p:=2; while(f < 2 and p*p <= n){
Line 2,993:
}
return(f + (n > 1) == 2);
}</
{{out}}
<pre>
|