11
\$\begingroup\$

As of time of submission to the sandbox, I had 17017 reputation, which you will all be able to factorise as 7×11×13×17, a product of 4 consecutive primes.

Please write a function or program to output all the products of two or more consecutive primes up to an input integer n. For example, given n=143 you should output 6, 15, 30, 35, 77, 105, 143 (or equivalent output format).

Normal restrictions on I/O and loopholes apply.

This is , so the shortest program wins.

\$\endgroup\$
  • 4
    \$\begingroup\$ oeis.org/A097889 \$\endgroup\$ – Robert Fraser Sep 4 '16 at 19:21
  • 2
    \$\begingroup\$ Should the output be sorted or not? \$\endgroup\$ – Fatalize Sep 4 '16 at 19:47
  • \$\begingroup\$ @Fatalize I had meant it to be sorted, but I see I didn't specify it well enough and there are already several answers which don't output a sorted list. \$\endgroup\$ – Neil Sep 4 '16 at 23:12
6
\$\begingroup\$

Jelly, 14 10 bytes

(no doubt there is golfing to do here! - yep...)
-4 bytes thanks to @Dennis - replace check for greater than n by using a range

ÆRẆP€ḟÆRfR

Note - this is both extremely inefficient and the results are unsorted.

Test it at TryItOnline

How?

ÆRẆP€ḟÆRfR - main link takes an argument, n
ÆR    ÆR   - primes up to n
  Ẇ        - all sublists
   P€      - product for each
     ḟ     - filter out the primes (since the sublists include those of lnegth 1)
        fR - filter out any not in range [1,N]
             (yep, it's calculating all products of primes up to n - gross)
\$\endgroup\$
  • 2
    \$\begingroup\$ You don't need µ and ³; >Ðḟ works just fine on its own. fR is even shorter. \$\endgroup\$ – Dennis Sep 4 '16 at 20:00
  • \$\begingroup\$ @Dennis - I await your superior method. Thanks! \$\endgroup\$ – Jonathan Allan Sep 4 '16 at 20:14
4
\$\begingroup\$

MATL, 25 20 bytes

Zq&Xf"@gnq?2MpG>~?6M

Approach similar to that in Jonathan Allan's answer.

Try it online!


Old version, 25 bytes

:YF!"@2<@sq0@0hhdz2=v?X@D

This obtains the exponents of prime factor decomposition for all numbers from 1 to the input. For each it checks:

  1. If all exponents are less than 2.
  2. If the sum of all exponents is greater than 1.
  3. The array of exponents is extended with an additional zero on each end. The consecutive differences of the extended array are computed. There should be exactly 2 nonzero differences.

If the three conditions are fulfilled the number is displayed. Results are in incresing order.

\$\endgroup\$
4
\$\begingroup\$

Javascript (ES6), 105 104 bytes

n=>{for(i=1,P=[];i++<n;P[P.every(v=>i%v)?i:n]=i);P.map(i=>P.map(j=>j>i&&(p*=j)<=n&&console.log(p),p=i))}

Demo

var f =
n=>{for(i=1,P=[];i++<n;P[P.every(v=>i%v)?i:n]=i);P.map(i=>P.map(j=>j>i&&(p*=j)<=n&&console.log(p),p=i))}

f(143)

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 17 15 bytes

L<ØŒ€PD¹>‹ÏDp_Ï

Explanation

L<Ø                 # get the first N primes, where N is the input
   Π               # get all combinations of consecutive primes
    €P              # calculate the product of these sublists
      D¹>‹Ï         # keep only the products less than or equal to N
           Dp_Ï     # keep only those that aren't prime

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pyth, 18 bytes

f}PTftlY.:fP_YSQ)S

A program that takes input of an integer on STDIN and prints a list of integers.

Try it online

How it works

f}PTftlY.:fP_YSQ)S  Program. Input: Q
              SQ    Yield [1, 2, 3, ..., Q]
          fP_Y      Filter that by primality
        .:      )   Yield all sublists of that
    f               Filter the sublists by:
      lY             Length
     t               -1
                    removing sublists of length 1
f                S  Filter [1, 2, 3, ..., Q] (implicit input fill) by:
  PT                 Prime factorisation
 }                   is in the sublists
                    Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Jelly, 11 bytes

ÆfÆCI=1Ȧµ€T

Not the shortest Jelly answer, but this approach is rather efficient and the output is sorted.

Try it online!

How it works

ÆfÆCI=1Ȧµ€T  Main link. Argument: n

        µ€   Map the preceding chain over each k in [1, ..., n].
Æf             Compute all prime factors of k, with multiplicities.
  ÆC           Count the number of primes less than or equal to each prime factor.
               This maps the j-th to j.
    I          Increments; compute the forward differences of consecutive indices.
     =1        Compare each difference with 1.
       Ȧ       All; return 1 iff the array is non-empty and has no zeroes.
          T  Truth; yield all indices for which the chain returned 1.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.