Odd squarefree semiprimes: Difference between revisions

m
syntax highlighting fixup automation
(Odd squarefree semiprimes en Python)
m (syntax highlighting fixup automation)
Line 9:
{{trans|C++}}
 
<langsyntaxhighlight lang="11l">F odd_square_free_semiprime(=n)
I n % 2 == 0
R 0B
Line 30:
print()
 
print("\nCount: "count)</langsyntaxhighlight>
 
{{out}}
Line 51:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
INCLUDE "H6:SIEVE.ACT"
 
Line 83:
OD
PrintF("%E%EThere are %I odd numbers",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Odd_squarefree_semiprimes.png Screenshot from Atari 8-bit computer]
Line 99:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find some odd square free semi-primes #
# numbers of the form p*q where p =/= q and p, q are prime #
# reurns a list of primes up to n #
Line 139:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 156:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">primes: select 0..1000 => prime?
lst: sort unique flatten map primes 'p [
map select primes 'q -> all? @[odd? p*q p<>q 1000>p*q]=>[p*&]
]
loop split.every:10 lst 'a ->
print map a => [pad to :string & 4]</langsyntaxhighlight>
 
{{out}}
Line 187:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ODD_SQUAREFREE_SEMIPRIMES.AWK
# converted from C++
Line 214:
return(count==1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 245:
Use the function from [[Primality by trial division#FreeBASIC]] as an include. This code generates the odd squarefree semiprimes in ascending order of their first factor, then their second.
 
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
dim as integer p, q
for p = 3 to 999
Line 253:
print p*q;" ";
next q
next p</langsyntaxhighlight>
{{out}}<pre> 15 21 33 39 51 57 69 87 93 111 123 129 141 159 177 183 201 213 219 237 249 267 291 303 309 321 327 339 381 393 411 417 447 453 471 489 501 519 537 543 573 579 591 597 633 669 681 687 699 717 723 753 771 789 807 813 831 843 849 879 921 933 939 951 993 35 55 65 85 95 115 145 155 185 205 215 235 265 295 305 335 355 365 395 415 445 485 505 515 535 545 565 635 655 685 695 745 755 785 815 835 865 895 905 955 965 985 995 77 91 119 133 161 203 217 259 287 301 329 371 413 427 469 497 511 553 581 623 679 707 721 749 763 791 889 917 959 973 143 187 209 253 319 341 407 451 473 517 583 649 671 737 781 803 869 913 979 221 247 299 377 403 481 533 559 611 689 767 793 871 923 949 323 391 493 527 629 697 731 799 901 437 551 589 703 779 817 893 667 713 851 943 989 899</pre>
 
==={{header|Tiny BASIC}}===
<langsyntaxhighlight lang="tinybasic"> LET P = 1
10 LET P = P + 2
LET Q = P
Line 278:
LET I = I + 1
IF I*I <= A THEN GOTO 110
RETURN</langsyntaxhighlight>
 
=={{header|C#|CSharp}}==
This reveals a set of semi-prime numbers (with exactly two factors for each '''<big>''n''</big>'''), where '''<big>''1 < p < q < n''</big>'''. It is square-free, since '''<big>''p < q''</big>'''.
<langsyntaxhighlight lang="csharp">using System; using static System.Console; using System.Collections;
using System.Linq; using System.Collections.Generic;
 
Line 298:
if (!flags[j]) { yield return j;
for (int k = sq, i = j << 1; k <= lim; k += i) flags[k] = true; }
for (; j <= lim; j += 2) if (!flags[j]) yield return j; } }</langsyntaxhighlight>
{{out}}
<pre> 15 21 33 35 39 51 55 57 65 69 77 85 87 91 93 95 111 115 119 123
Line 314:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 344:
std::cout << "\nCount: " << count << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 364:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit formatting grouping io kernel
math.primes.factors math.ranges prettyprint sequences sets ;
 
Line 375:
999 odd-sfs-upto dup length
"Found %d odd square-free semiprimes < 1000:\n" printf
20 group [ [ "%4d" printf ] each nl ] each nl</langsyntaxhighlight>
{{out}}
<pre>
Line 393:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: odd-square-free-semi-prime? { n -- ? }
n 1 and 0= if false exit then
0 { count }
Line 427:
 
1000 special_odd_numbers
bye</langsyntaxhighlight>
 
{{out}}
Line 448:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 477:
}
fmt.Printf("\n\n%d such numbers found.\n", len(oss))
}</langsyntaxhighlight>
 
{{out}}
Line 512:
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable definition of `is_prime`.
 
<langsyntaxhighlight lang="jq"># Output: a stream of proper square-free odd prime factors of .
def proper_odd_squarefree_prime_factors:
range(3; 1 + sqrt|floor) as $i
Line 534:
| select(is_odd_squarefree_semiprime)]
| nwise(10)
| map(lpad(3)) | join(" ")</langsyntaxhighlight>
{{out}}
As for [[#Arturo|Arturo]], [[#Wren|Wren]], et al.
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
twoprimeproduct(n) = (a = factor(n).pe; length(a) == 2 && all(p -> p[2] == 1, a))
Line 546:
 
foreach(p -> print(rpad(p[2], 4), p[1] % 20 == 0 ? "\n" : ""), enumerate(special1k))
</langsyntaxhighlight>{{out}}
<pre>
15 21 33 35 39 51 55 57 65 69 77 85 87 91 93 95 111 115 119 123
Line 560:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">n = 1000; primes = Most@NestWhileList[NextPrime, 3, # < n/3 &];
reduceList[x_List, n_] := TakeWhile[Rest[x], # < n/First[x] &];
q = Rest /@
NestWhileList[reduceList[#, n] &, primes, Length@# > 2 &];
semiPrimes = Sort@Flatten@MapThread[Times, {q, primes[[;; Length@q]]}];
Partition[TakeWhile[semiPrimes, # < 1000 &], UpTo[10]] // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 591:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">#!/bin/ksh
 
# Odd squarefree semiprimes
Line 679:
print ${osfsp[*]}
echo
print "Counted ${#osfsp[*]} odd squarefree semiprimes under 1000"</langsyntaxhighlight>
{{out}}<pre>15 21 33 35 39 51 55 57 65 69 77 85 87 91 93 95 111 115 119 123 129 133 141 143
145 155 159 161 177 183 185 187 201 203 205 209 213 215 217 219 221 235 237 247
Line 695:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, strutils, sugar
 
const
Line 726:
for i, n in result:
stdout.write ($n).align(3), if (i + 1) mod 20 == 0: '\n' else: ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 741:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">for(s=3, 999, f=factor(s); m=matsize(f); if(s%2==1&&m[1]==2&&f[1,2]==1&&f[2,2]==1, print(s)))</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Odd_squarefree_semiprimes
Line 751:
my (@primes, @found) = grep $_ & 1 && (1 x $_) !~ /^(11+)\1+$/, 3 .. 999 / 3;
"@primes" =~ /\b(\d+)\b.*?\b(\d+)\b(?{ $found[$1 * $2] = $1 * $2 })(*FAIL)/;
print "@{[ grep $_, @found[3 .. 999] ]}\n" =~ s/.{75}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 767:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">function</span> <span style="color: #000000;">oss</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: #004080;">sequence</span> <span style="color: #000000;">f</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>
Line 775:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d odd square-free semiprimes less than 1,000:\n %s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 784:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
Line 800:
if not isPrime(q):
continue
print(p*q, end = " ");</langsyntaxhighlight>
 
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>say (3..333).grep(*.is-prime).combinations(2)».map( * * * ).flat\
.grep( * < 1000 ).sort.batch(20)».fmt('%3d').join: "\n";</langsyntaxhighlight>
{{out}}
<pre> 15 21 33 35 39 51 55 57 65 69 77 85 87 91 93 95 111 115 119 123
Line 819:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm finds odd squarefree semiprimes (product of 2 primes) that are less then N. */
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /* " " " " " " */
Line 865:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j /*bump # Ps; assign next P; P squared*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 896:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">load "stdlib.ring" # for isprime() function
? "working..." + nl + "Odd squarefree semiprimes are:"
Line 933:
s = string(x) l = len(s)
if l > y y = l ok
return substr(" ", 11 - y + l) + s</langsyntaxhighlight>
{{out}}
<pre>working...
Line 953:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func odd_squarefree_almost_primes(upto, k=2) {
k.squarefree_almost_primes(upto).grep{.is_odd}
}
Line 961:
say "Found #{list.len} odd square-free semiprimes <= #{n.commify}:"
say (list.first(10).join(', '), ', ..., ', list.last(10).join(', '))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 973:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/seq" for Lst
import "/fmt" for Fmt
Line 990:
System.print("Odd squarefree semiprimes under 1,000:")
for (chunk in Lst.chunks(oss, 10)) Fmt.print("$3d", chunk)
System.print("\n%(oss.count) such numbers found.")</langsyntaxhighlight>
 
{{out}}
Line 1,020:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,049:
Text(0, " odd squarefree semiprimes found below 1000.
");
]</langsyntaxhighlight>
 
{{out}}
10,327

edits