How to resolve the algorithm Sphenic numbers step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Sphenic numbers step by step in the J programming language

Table of Contents

Problem Statement

A sphenic number is a positive integer that is the product of three distinct prime numbers. More technically it's a square-free 3-almost prime (see Related tasks below). For the purposes of this task, a sphenic triplet is a group of three sphenic numbers which are consecutive. Note that sphenic quadruplets are not possible because every fourth consecutive positive integer is divisible by 4 (= 2 x 2) and its prime factors would not therefore be distinct. 30 (= 2 x 3 x 5) is a sphenic number and is also clearly the first one. [1309, 1310, 1311] is a sphenic triplet because 1309 (= 7 x 11 x 17), 1310 (= 2 x 5 x 31) and 1311 (= 3 x 19 x 23) are 3 consecutive sphenic numbers. Calculate and show here:

  1. All sphenic numbers less than 1,000.
  2. All sphenic triplets less than 10,000.
  3. How many sphenic numbers are there less than 1 million?
  4. How many sphenic triplets are there less than 1 million?
  5. What is the 200,000th sphenic number and its 3 prime factors?
  6. What is the 5,000th sphenic triplet? Hint: you only need to consider sphenic numbers less than 1 million to answer 5. and 6.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sphenic numbers step by step in the J programming language

Source code in the j programming language

sphenic=: {{ N #~ N = {{*/~.3{.y}}@q: N=. 30}.i.y }}
triplet=: {{ 0 1 2 +/~y #~ */y e.~ 0 1 2 +/ y }}


   9 15$sphenic 1e3
 30  42  66  70  78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
   triplet sphenic 1e4
1309 1310 1311
1885 1886 1887
2013 2014 2015
2665 2666 2667
3729 3730 3731
5133 5134 5135
6061 6062 6063
6213 6214 6215
6305 6306 6307
6477 6478 6479
6853 6854 6855
6985 6986 6987
7257 7258 7259
7953 7954 7955
8393 8394 8395
8533 8534 8535
8785 8786 8787
9213 9214 9215
9453 9454 9455
9821 9822 9823
9877 9878 9879
   # sphenic 1e6
206964
   # triplet sphenic 1e6
5457
   4999 { triplet sphenic 1e6   NB. 0 is first
918005 918006 918007


  

You may also check:How to resolve the algorithm Greatest element of a list step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Delegates step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm List comprehensions step by step in the Ioke programming language
You may also check:How to resolve the algorithm File input/output step by step in the GAP programming language
You may also check:How to resolve the algorithm A+B step by step in the REBOL programming language