Odd squarefree semiprimes: Difference between revisions

(J draft)
(14 intermediate revisions by 5 users not shown)
Line 640:
=={{header|J}}==
 
<syntaxhighlight lang=J> (#~ 1000>])*/"1 p:1+2 comb p:inv 1000%3require'stats'
(#~ 1000>])*/"1 p:1+2 comb p:inv 1000%3
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</syntaxhighlight>
 
Note that these values are sorted in order of their smallest prime factor followed by the larger prime factor. In other words, since 899 is 29*31, and there's 8 odd primes smaller than 29, there are (conceptually speaking) 9 ascending subsequences of semiprimes here.
 
=={{header|jq}}==
Line 695 ⟶ 698:
817 831 835 843 849 851 865 869 871 879 889 893 895 899 901 905 913 917 921 923
933 939 943 949 951 955 959 965 973 979 985 989 993 995
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="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</syntaxhighlight>
 
{{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 249 253 259 265 267 287
291 295 299 301 303 305 309 319 321 323
327 329 335 339 341 355 365 371 377 381
391 393 395 403 407 411 413 415 417 427
437 445 447 451 453 469 471 473 481 485
489 493 497 501 505 511 515 517 519 527
533 535 537 543 545 551 553 559 565 573
579 581 583 589 591 597 611 623 629 633
635 649 655 667 669 671 679 681 685 687
689 695 697 699 703 707 713 717 721 723
731 737 745 749 753 755 763 767 771 779
781 785 789 791 793 799 803 807 813 815
817 831 835 843 849 851 865 869 871 879
889 893 895 899 901 905 913 917 921 923
933 939 943 949 951 955 959 965 973 979
985 989 993 995
</pre>
 
Line 829 ⟶ 802:
 
Counted 194 odd squarefree semiprimes under 1000
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="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</syntaxhighlight>
 
{{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 249 253 259 265 267 287
291 295 299 301 303 305 309 319 321 323
327 329 335 339 341 355 365 371 377 381
391 393 395 403 407 411 413 415 417 427
437 445 447 451 453 469 471 473 481 485
489 493 497 501 505 511 515 517 519 527
533 535 537 543 545 551 553 559 565 573
579 581 583 589 591 597 611 623 629 633
635 649 655 667 669 671 679 681 685 687
689 695 697 699 703 707 713 717 721 723
731 737 745 749 753 755 763 767 771 779
781 785 789 791 793 799 803 807 813 815
817 831 835 843 849 851 865 869 871 879
889 893 895 899 901 905 913 917 921 923
933 939 943 949 951 955 959 965 973 979
985 989 993 995
</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
block(
[count:0,oddsemiprime:[]],
while count<1000 do (
i:lambda([x],oddp(x) and length(ifactors(x))=2 and unique(map(second,ifactors(x)))=[1])(count),
if i then oddsemiprime:endcons(count,oddsemiprime),
count:count+1),
oddsemiprime);
</syntaxhighlight>
{{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,249,253,259,265,267,287,291,295,299,301,303,305,309,319,321,323,327,329,335,339,341,355,365,371,377,381,391,393,395,403,407,411,413,415,417,427,437,445,447,451,453,469,471,473,481,485,489,493,497,501,505,511,515,517,519,527,533,535,537,543,545,551,553,559,565,573,579,581,583,589,591,597,611,623,629,633,635,649,655,667,669,671,679,681,685,687,689,695,697,699,703,707,713,717,721,723,731,737,745,749,753,755,763,767,771,779,781,785,789,791,793,799,803,807,813,815,817,831,835,843,849,851,865,869,871,879,889,893,895,899,901,905,913,917,921,923,933,939,943,949,951,955,959,965,973,979,985,989,993,995]
</pre>
 
Line 918 ⟶ 937:
15, 21, 33, 35, 39, ..., 979, 985, 989, 993, 995
</pre>
=={{header|Prolog}}==
works with swi-prolog
<syntaxhighlight lang="prolog">
oddPrimes(3, Limit):- 3 =< Limit.
oddPrimes(N, Limit):-
between(5, Limit, N),
N /\ 1 > 0, % odd
N mod 3 > 0, % \= 3*i
M is floor(sqrt(N)) + 1, % reverse 6*I-1
Max is M div 6,
forall(between(1, Max, I), (N mod (6*I-1) > 0, N mod (6*I+1) > 0)).
 
merge([], Sort, Sort).
merge([X|Sort1], [Y|Sort2], [X|Sort]):-
X < Y,!,
merge(Sort1, [Y|Sort2], Sort).
merge(Sort1, [Y|Sort2], [Y|Sort]):-
merge(Sort2, Sort1, Sort).
 
semiPrimes(PList, Limit, SpList):-
semiPrimes(PList, Limit, [], SpList).
 
semiPrimes([], _, Acc, Acc). % odd, squarefree generator
semiPrimes([P|PList], Limit, Acc, SpList):-
findall(Sp, (member(X, PList), X =< Limit div P, Sp is P * X), MList),
MList = [_|_],!,
merge(Acc, MList, Acc1),
semiPrimes(PList, Limit, Acc1, SpList).
semiPrimes(_, _, Acc, Acc).
 
showList(List):-
findnsols(20, X, (member(X, List), writef('%4r', [X])), _SubList), nl,
fail.
showList(_).
do:-Limit is 1000,
PLimit is Limit div 3,
findall(N, oddPrimes(N, PLimit), PList),
semiPrimes(PList, Limit, SpList),!,
showList(SpList).
</syntaxhighlight>
{{out}}
<pre>
?- time(do).
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 249 253 259 265 267 287 291 295 299 301 303 305 309 319 321 323
327 329 335 339 341 355 365 371 377 381 391 393 395 403 407 411 413 415 417 427
437 445 447 451 453 469 471 473 481 485 489 493 497 501 505 511 515 517 519 527
533 535 537 543 545 551 553 559 565 573 579 581 583 589 591 597 611 623 629 633
635 649 655 667 669 671 679 681 685 687 689 695 697 699 703 707 713 717 721 723
731 737 745 749 753 755 763 767 771 779 781 785 789 791 793 799 803 807 813 815
817 831 835 843 849 851 865 869 871 879 889 893 895 899 901 905 913 917 921 923
933 939 943 949 951 955 959 965 973 979 985 989 993 995
% 11,306 inferences, 0.005 CPU in 0.006 seconds (85% CPU, 2410378 Lips)
true.
</pre>
 
=={{header|Python}}==
Line 1,121 ⟶ 1,196:
Found 194 Odd squarefree semiprimes.
done...
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
≪ → max
≪ { } 3
'''DO'''
3 1 CF
'''WHILE''' DUP2 > 1 FC? AND '''REPEAT'''
DUP2 *
'''IF''' DUP max ≤ '''THEN''' 4 ROLL + UNROT '''ELSE''' DROP 1 SF '''END'''
NEXTPRIME
'''END'''
DROP NEXTPRIME
'''UNTIL''' DUP 3 * max > '''END'''
DROP SORT
≫ ≫ '<span style="color:blue">OSSP</span>' STO
 
1000 <span style="color:blue">OSSP</span>
{{out}}
<pre>
1: {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 249 253 259 265 267 287 291 295 299 301 303 305 309 319 321 323 327 329 335 339 341 355 365 371 377 381 391 393 395 403 407 411 413 415 417 427 437 445 447 451 453 469 471 473 481 485 489 493 497 501 505 511 515 517 519 527 533 535 537 543 545 551 553 559 565 573 579 581 583 589 591 597 611 623 629 633 635 649 655 667 669 671 679 681 685 687 689 695 697 699 703 707 713 717 721 723 731 737 745 749 753 755 763 767 771 779 781 785 789 791 793 799 803 807 813 815 817 831 835 843 849 851 865 869 871 879 889 893 895 899 901 905 913 917 921 923 933 939 943 949 951 955 959 965 973 979 985 989 993 995}
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
require 'prime'
 
res = (1..1000).step(2).select {|n| n.prime_division.map(&:last) == [1, 1] }
res.each_slice(20){|slice| puts "%4d"*slice.size % slice}
puts "\nCount: #{res.count}"</syntaxhighlight>
{{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 249 253 259 265 267 287 291 295 299 301 303 305 309 319 321 323
327 329 335 339 341 355 365 371 377 381 391 393 395 403 407 411 413 415 417 427
437 445 447 451 453 469 471 473 481 485 489 493 497 501 505 511 515 517 519 527
533 535 537 543 545 551 553 559 565 573 579 581 583 589 591 597 611 623 629 633
635 649 655 667 669 671 679 681 685 687 689 695 697 699 703 707 713 717 721 723
731 737 745 749 753 755 763 767 771 779 781 785 789 791 793 799 803 807 813 815
817 831 835 843 849 851 865 869 871 879 889 893 895 899 901 905 913 917 921 923
933 939 943 949 951 955 959 965 973 979 985 989 993 995
 
Count: 194
</pre>
 
=={{header|Scala}}==
Scala3 ready
<syntaxhighlight lang="scala">
val primes3 = 3 #:: LazyList.from(5, 6)
.flatMap(p => Iterator(p, p + 2))
.filter(p => (5 to math.sqrt(p).floor.toInt by 6).forall(a => p % a > 0 && p % (a + 2) > 0))
 
def merge(sort1: Seq[Int], sort2: Seq[Int]): Seq[Int] = {
def iter(sort1: Seq[Int], sort2: Seq[Int], acc: Seq[Int]): Seq[Int] = {
if (sort1.isEmpty) return acc ++ sort2
val (x, y) = (sort1.head, sort2.head)
val (min, s1, s2) = if (x < y) (x, sort1.tail, sort2)
else (y, sort2.tail, sort1)
iter(s1, s2, acc :+ min)
}
iter(sort1, sort2, Seq())
}
 
def semiPrimes(limit: Int): Seq[Int] = { // odd, squarefree generator
def iter(primeList: Seq[Int], acc: Seq[Int]): Seq[Int] = {
val (start, primeList1) = (primeList.head, primeList.tail)
val subList = primeList1.map(_ * start).takeWhile(_ <= limit)
if (subList.isEmpty) return acc
iter(primeList1, merge(acc, subList))
}
iter(primes3, Seq())
}
 
@main def main = {
val start = System.currentTimeMillis
val spList = semiPrimes(1000)
val duration = System.currentTimeMillis - start
spList.grouped(20).foreach(group =>
group.foreach(sp => print(f"$sp%3d "))
println
)
println(s"number: ${spList.length} [time elapsed: $duration ms]")
}
</syntaxhighlight>
{{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 249 253 259 265 267 287 291 295 299 301 303 305 309 319 321 323
327 329 335 339 341 355 365 371 377 381 391 393 395 403 407 411 413 415 417 427
437 445 447 451 453 469 471 473 481 485 489 493 497 501 505 511 515 517 519 527
533 535 537 543 545 551 553 559 565 573 579 581 583 589 591 597 611 623 629 633
635 649 655 667 669 671 679 681 685 687 689 695 697 699 703 707 713 717 721 723
731 737 745 749 753 755 763 767 771 779 781 785 789 791 793 799 803 807 813 815
817 831 835 843 849 851 865 869 871 879 889 893 895 899 901 905 913 917 921 923
933 939 943 949 951 955 959 965 973 979 985 989 993 995
number: 194 [time elapsed: 6 ms]
</pre>
 
Line 1,141 ⟶ 1,313:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascript">import "./mathfmt" for IntFmt
import "/seq" for Lst
import "/fmt" for Fmt
import "/sort" for Sort
 
var primes = Int.primeSieve(333)
Line 1,158 ⟶ 1,326:
}
}
Sort.quick(oss)
System.print("Odd squarefree semiprimes under 1,000:")
Fmt.tprint("$3d", oss.sort(), 10)
for (chunk in Lst.chunks(oss, 10)) Fmt.print("$3d", chunk)
System.print("\n%(oss.count) such numbers found.")</syntaxhighlight>
 
9,476

edits