22
\$\begingroup\$

A while ago, I had a look at the prime factorization of 27000:

27000 = 23 × 33 × 53

There are two special things about that:

  • consecutive-prime: The primes are consecutive: 2 is the 1st prime, 3 is the 2nd prime, 5 is the 3rd prime.
  • constant-exponent: The exponent is the same for every prime (always 3)

Mathematically expressed:

An integer x is a consecutive-prime/constant-exponent number if there exist strictly positive integers n, k, m such that x = pnm × pn+1m × ... × pn+km, where pj is the j-th prime

Your task is to test if a positive integer fulfills these conditions.

Input:

A positive integer > 1, in any reasonable form.

Output:

One of two values, at least one of which has to be constant, indicating whether the input is a consecutive-prime/constant-exponent number.

Edge cases:

  • primes are truthy, as the factorization for prime p is p1
  • other numbers that can be written as pm where p is a prime are also truthy.

Rules:

  • Standard loopholes apply.
  • No worries about integer overflow, but numbers up to 255 must work.
  • Shortest code in bytes wins.

Test cases:

Truthy:

2
3
4
5
6
7
8
9
11
13
15
27000
456533

Falsy:

10
12
14
72
10000000

Here is a python script generating some test cases.

The fact that I accepted an answer does not mean that the challenge is over; the winner can still change!

\$\endgroup\$
  • \$\begingroup\$ You could probably come at this the other way by generating a list of all such numbers and checking if the input is in the list \$\endgroup\$ – Engineer Toast May 16 '18 at 15:48
  • \$\begingroup\$ @EngineerToast There are infinitely many truthy numbers though. \$\endgroup\$ – Alexis Olson May 16 '18 at 21:58
  • \$\begingroup\$ @AlexisOlson Sure, but a finite that can be handled as integers by many languages. \$\endgroup\$ – Engineer Toast May 16 '18 at 22:11
  • \$\begingroup\$ Your mathematical expression has Pj doesn't relate to the x = Pn^m part. I'm assuming you meant Pn is the n-th prime \$\endgroup\$ – Veskah May 16 '18 at 22:37
  • \$\begingroup\$ @Veskah n has a specific value (index of the first prime dividing x), so saying Pn is the n-th prime is awkward if you also want to imply that Pn+1 is the n+1-th prime. \$\endgroup\$ – Dennis May 17 '18 at 3:53

19 Answers 19

13
\$\begingroup\$

05AB1E, 4 bytes

Ó0ÛË

Try it online!

Explanation

Ó     # get a list of prime exponents
 0Û   # remove leading zeroes
   Ë  # all remaining elements are equal
\$\endgroup\$
  • \$\begingroup\$ ÎÓKË is all I can think of other than this, nice one... I was thinking ß but it does the opposite of what I thought. \$\endgroup\$ – Magic Octopus Urn May 18 '18 at 1:53
7
\$\begingroup\$

Regex (ECMAScript), 276 205 201 193 189 bytes

Comparing the multiplicities (exponents) of different prime factors is an interesting problem for solving with ECMAScript regex – the lack of backreferences that persist through iterations of a loop makes it a challenge to count anything. Even if counting the numerical trait in question is possible, often a more indirect approach makes for better golf.

As with my other ECMA regex posts, I'll give a spoiler warning: I highly recommend learning how to solve unary mathematical problems in ECMAScript regex. It's been a fascinating journey for me, and I don't want to spoil it for anybody who might potentially want to try it themselves, especially those with an interest in number theory. See this earlier post for a list of consecutively spoiler-tagged recommended problems to solve one by one.

So do not read any further if you don't want some advanced unary regex magic spoiled for you. If you do want to take a shot at figuring out this magic yourself, I highly recommend starting by solving some problems in ECMAScript regex as outlined in that post linked above.

The main payload from a regex I previously developed turned out to be very applicable to this challenge. That is the regex that finds the prime(s) of highest multiplicity. My first solution for that was very long, and I later golfed it way down in stages, first rewriting it to use molecular lookahead, and then porting it back to plain ECMAScript using an advanced technique to work around the lack of molecular lookahead, and subsequently golfing it down to be much smaller than the original plain ECMAScript solution.

The part from that regex that applies to this problem is the first step, which finds Q, the smallest factor of N that shares all of its prime factors. Once we have this number, all we have to do to show that N is a "constant-exponent number" is divide N by Q until we can't any longer; if the result is 1, all primes are of equal multiplicity.

After submitting an answer using my previously developed algorithm for finding Q, I realized that it could be calculated in an entirely different way: Find the largest square-free factor of N (using the same algorithm as my Carmichael number regex). As it turns out, this poses no difficulty at all* in terms of stepping around the lack of molecular lookahead and variable-length lookbehind (no need to pull in the advanced technique previously used), and is 64 bytes shorter! Additionally it eliminates the complexity of treating square-free N and prime N as different special cases, dropping another 7 bytes from this solution.

(There still remain other problems that require the advanced technique formerly used here to golf down the calculation of Q, but currently none of them are represented by my PPCG posts.)

I put the multiplicity test before the consecutive-primes test because the latter is much slower; putting tests that can fail more quickly first makes the regex faster for uniformly distributed input. It's also better golf to put it first, because it uses more backreferences (which would cost more if they were double-digit).

I was able to drop 4 bytes from this regex (193 → 189) using a trick found by Grimy that can futher shorten division in the case that the quotient is guaranteed to be greater than or equal to the divisor.

^(?=(|(x+)\2*(?=\2$))((?=(xx+?)\4*$)(?=(x+)(\5+$))\6(?!\4*$))*x$)(?=.*$\2|((?=((x*)(?=\2\9+$)x)(\8*$))\10)*x$)(?!(((x+)(?=\13+$)(x+))(?!\12+$)(x+))\11*(?=\11$)(?!(\15\14?)?((xx+)\18+|x?)$))

Try it online!

# For the purposes of these comments, the input number = N.
^

# Assert that all of N's prime factors are of equal multiplicity
# Step 1: Find Q, the largest square-free factor of N (which will also be the smallest
# factor of N that has all the same prime factors as N) and put it in \2.
# If N is square-free, \2 will be unset.
(?=
    # Search through all factors of N, from largest to smallest, searching for one that
    # satisfies the desired property. The first factor tried will be N itself, for which
    # \2 will be unset.
    (|(x+)\2*(?=\2$))     # for factors < N: \2 = factor of N; tail = \2
    # Assert that tail is square-free (its prime factors all have single multiplicity)
    (
        (?=(xx+?)\4*$)    # \4 = smallest prime factor of tail
        (?=(x+)(\5+$))    # \5 = tail / \4 (implicitly); \6 = tool to make tail = \5
        \6                # tail = \5
        (?!\4*$)          # Assert that tail is no longer divisible by \4, i.e. that that
                          # prime factor was of exactly single multiplicity.
    )*x$
)
# Step 2: Require that either \2 is unset, or that the result of repeatedly
# dividing tail by \2 is 1.
(?=
    .*$\2
|
    (
        # In the following division calculation, we can skip the test for divisibility
        # by \2-1 because it's guaranteed that \2 <= \8. As a result, we did not need to
        # capture \2-1 above, and can use a better-golfed form of the division.
        (?=
            (              # \8 = tail / \2
                (x*)       # \9 = \8-1
                (?=\2\9+$)
                x
            )
            (\8*$)         # \10 = tool to make tail = \8
        )
        \10               # tail = \8
    )*
    x$                    # Require that the end result is 1
)

# Assert that there exists no trio of prime numbers such that N is divisible by the
# smallest and largest prime but not the middle prime.
(?!
    (                          # \11 = a factor of N
        (                      # \12 = a non-factor of N between \11 and \13
            (x+)(?=\13+$)      # \13 = a factor of N smaller than \11
            (x+)               # \14 = tool (with \15) to make tail = \13
        )
        (?!\12+$)
        (x+)                   # \15 = tool to make tail = \12
    )
    \11*(?=\11$)               # tail = \11

    # Assert that \11, \12, and \13 are all prime
    (?!
        (\15\14?)?             # tail = either \11, \12, or \13
        ((xx+)\18+|x?)$
    )
)


* It is still cleaner with molecular lookahead, with no special case for N being square-free. This drops 6 bytes, yielding a 195 187 183 byte solution:

^(?=(?*(x+))\1*(?=\1$)((?=(xx+?)\3*$)(?=(x+)(\4+$))\5(?!\3*$))*x$)(?=((?=((x*)(?=\1\8+$)x)(\7*$))\9)*x$)(?!(((x+)(?=\12+$)(x+))(?!\11+$)(x+))\10*(?=\10$)(?!(\14\13?)?((xx+)\17+|x?)$))

# For the purposes of these comments, the input number = N.
^

# Assert that all of N's prime factors are of equal multiplicity
# Step 1: Find Q, the largest square-free factor of N (which will also be the smallest
# factor of N that has all the same prime factors as N) and put it in \1.
(?=
    (?*(x+))              # \1 = proposed factor of N
    \1*(?=\1$)            # Assert that \1 is a factor of N; tail = \1
    # Assert that tail is square-free (its prime factors all have single multiplicity)
    (
        (?=(xx+?)\3*$)    # \3 = smallest prime factor of tail
        (?=(x+)(\4+$))    # \4 = tail / \3 (implicitly); \5 = tool to make tail = \4
        \5                # tail = \4
        (?!\3*$)          # Assert that tail is no longer divisible by \3, i.e. that that
                          # prime factor was of exactly single multiplicity.
    )*x$
)
# Step 2: Require that the result of repeatedly dividing tail by \1 is 1.
(?=
    (
        # In the following division calculation, we can skip the test for divisibility
        # by \1-1 because it's guaranteed that \2 <= \8. As a result, we did not need to
        # capture \1-1 above, and can use a better-golfed form of the division.
        (?=
            (             # \7 = tail / \1
                (x*)      # \8 = \7-1
                (?=\1\8+$)
                x
            )
            (\7*$)        # \9 = tool to make tail = \7
        )
        \9                # tail = \7
    )*
    x$                    # Require that the end result is 1
)

# Assert that there exists no trio of prime numbers such that N is divisible by the
# smallest and largest prime but not the middle prime.
(?!
    (                          # \10 = a factor of N
        (                      # \11 = a non-factor of N between \10 and \12
            (x+)(?=\12+$)      # \12 = a factor of N smaller than \10
            (x+)               # \13 = tool (with \14) to make tail = \12
        )
        (?!\11+$)
        (x+)                   # \14 = tool to make tail = \11
    )
    \10*(?=\10$)               # tail = \10

    # Assert that \10, \11, and \12 are all prime
    (?!
        (\14\13?)?             # tail = either \10, \11, or \12
        ((xx+)\17+|x?)$
    )
)

Here it is ported to variable-length lookbehind:

Regex (ECMAScript 2018), 198 195 194 186 182 bytes

^(?=(x+)(?=\1*$)(?<=^x((?<!^\5*)\3(?<=(^\4+)(x+))(?<=^\5*(x+?x)))*))((?=((x*)(?=\1\8+$)x)(\7*$))\9)*x$(?<!(?!(\14\16?)?((xx+)\12+|x?)$)(?<=^\13+)((x+)(?<!^\15+)((x+)(?<=^\17+)(x+))))

Try it online!

# For the purposes of these comments, the input number = N.
^

# Assert that all of N's prime factors are of equal multiplicity
# Step 1: Find Q, the largest square-free factor of N (which will also be the smallest
# factor of N that has all the same prime factors as N) and put it in \1.
(?=
    (x+)(?=\1*$)      # \1 = factor of N; head = \1
    (?<=              # This is evaluated right-to-left, so please read bottom to top.
        ^x
        (
            (?<!^\5*)        # Assert that head is no longer divisible by \6, i.e. that
                             # that prime factor was of exactly single multiplicity.
            \3               # head = \4
            (?<=(^\4+)(x+))  # \4 = head / \5 (implicitly); \3 = tool to make head = \4
            (?<=^\5*(x+?x))  # \5 = smallest prime factor of head
        )*
    )
)
# Step 2: Require that the result of repeatedly dividing tail by \1 is 1.
(
    # In the following division calculation, we can skip the test for divisibility
    # by \1-1 because it's guaranteed that \2 <= \8. As a result, we did not need to
    # capture \1-1 above, and can use a better-golfed form of the division.
    (?=
        (             # \7 = tail / \1
            (x*)      # \8 = \7-1
            (?=\1\8+$)
            x
        )
        (\7*$)        # \9 = tool to make tail = \7
    )
    \9                # tail = \7
)*
x$                    # Require that the end result is 1

# Assert that there exists no trio of prime numbers such that N is divisible by the
# smallest and largest prime but not the middle prime.
# This is evaluated right-to-left, so please read bottom to top, but switch back to
# reading top to bottom at the negative lookahead.
(?<!
    # Assert that \13, \15, and \17 are all prime.
    (?!
        (\14\16?)?           # tail = either \13, \15, or \17
        ((xx+)\12+|x?)$
    )

    (?<=^\13+)
    (                        # tail = \13
        (x+)                 # \14 = tool to make tail = \15
        (?<!^\15+)
        (
            (x+)             # \16 = tool (with \14) to make tail = \17
            (?<=^\17+)(x+)   # \17 = a factor of N smaller than \13
        )                    # \15 = a non-factor of N between \13 and \17
    )                        # \13 = a factor of N
)
\$\endgroup\$
  • \$\begingroup\$ You can replace .*$\2 with \2^ \$\endgroup\$ – H.PWiz Apr 22 at 5:36
  • \$\begingroup\$ Although, I believe this is valid: ^(?=(|(x+)\2*(?=\2$))(((?=(xx+?)\5*$)(?=(x+)(\6+$))\7(?!\5*$))*x$))(?!(((xx+)(?=\10+$)(x+))(?!\9+$)(x+))\8*(?=\8$)(?!(\12\11?)?(xx+)\14+$))((?=((x*)(?=\2\17+$)x)(\16*$))\19)*\3$ \$\endgroup\$ – H.PWiz Apr 22 at 6:28
  • \$\begingroup\$ Doesn't seem close to optimal though \$\endgroup\$ – H.PWiz Apr 22 at 6:29
6
\$\begingroup\$

Jelly, 13 6 5 bytes

ÆEt0E

Try it online!

Still outgolfed... (thanks Erik for -1 byte)


Explanation

ÆE     # get a list of prime exponents (noooo long builtin name)
  t0   # remove zeroes on both sides (leading or trailing)
    E  # all remaining elements are equal
\$\endgroup\$
  • \$\begingroup\$ œl -> t. There's no reason for trailing zeroes to be present in ÆE's output. \$\endgroup\$ – Erik the Outgolfer May 16 '18 at 16:01
  • \$\begingroup\$ This doesn't quite work but will maybe lead to a 4 byte solution. \$\endgroup\$ – dylnan May 16 '18 at 17:44
  • \$\begingroup\$ @dylnan That fails for 2250. \$\endgroup\$ – Dennis May 16 '18 at 17:49
  • \$\begingroup\$ @Dennis Thanks, I had realized it wouldn't work but I am hoping it will inspire a four byte solution \$\endgroup\$ – dylnan May 16 '18 at 18:04
6
\$\begingroup\$

JavaScript (ES6), 87 bytes

Returns 0 for truthy or a non-zero integer for falsy.

f=(n,k=2,j,i)=>n%k?j*(P=d=>k%--d?P(d):d==!i)(k)|j-i|(n>1&&f(n,k+1,j||i)):f(n/k,k,j,-~i)

Try it online!

Commented

f = (                     // f() = recursive function taking:
  n,                      //   n = input
  k = 2,                  //   k = current factor
  j,                      //   j = reference exponent, initially undefined
  i                       //   i = current exponent, undefined each time we start testing
) =>                      //       the next factor
  n % k ?                 // if k is not a divisor of n:
    j * (                 //   ignore the primality of k if j is still undefined
      P = d =>            //     P() = function testing if k is prime:
        k % --d ?         //       decrement d; if d is not a divisor of k:
          P(d)            //         do a recursive call until it is
        :                 //       else:
          d == !i         //         unless i is already defined: d must not be equal to 1
                          //         (if it is: k is the next prime but does not divide n)
    )(k) |                //   initial call to P() with d = k
    j - i | (             //   if both i and j are defined, they must be equal
      n > 1 &&            //   if n is not yet equal to 1,
      f(n, k + 1, j || i) //   go on with k + 1; if j is undefined, set it to i
    )                     //   (otherwise, stop recursion and return what we have)
  :                       // else:
    f(n / k, k, j, -~i)   //   increment the current exponent and go on with n / k
\$\endgroup\$
  • \$\begingroup\$ This was broken by the change of j||i to i. It now yields lots of false positives. \$\endgroup\$ – Deadcode Feb 11 at 18:03
  • \$\begingroup\$ @Deadcode I can't check or correct this for the moment, so I've just rollbacked for now. \$\endgroup\$ – Arnauld Feb 11 at 19:14
5
\$\begingroup\$

CJam, 30 29 bytes

{mFz~)-!\__W=,\0=>\-:mp1#W=&}

Try it online!

My first answer after a nearly 2(!)-year break, so it can probably be golfed more. This is a block that takes input as an integer (can also be mapped over for arrays of integers).

Explanation

{        e# Begin block
 mF      e# Factor input, giving an array of primes and their powers
 z~      e# Transpose and dump, giving an array of primes and an array of powers
 )-      e# Check that the powers are the same: subtract each power from the last element
 !       e# Negate to make sure they're all 0
 \__W=,  e# Get the range from 0 to the largest prime minus one
 \0=>    e# Slice that array so it only includes everything larger than the smallest prime
 \-      e# Remove the original primes from the range array
 :mp     e# Check each element for primality. If the input's primes are consecutive,
         e# this will contain no primes
 1#W=    e# Make sure a "1" is not found
 &       e# If the powers are the same AND primes are consecutive, return 1. Otherwise, 0.
}
\$\endgroup\$
5
\$\begingroup\$

Stax, 5 6 bytes

╣♥qJ╬c

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

|n    get the exponents of the prime factorization
0:D   trim leading zeroes
:u    array has exactly a single distinct element

Edit: This doesn't work on 512. I'll give it some thought and hopefully a fix later. It works now.

\$\endgroup\$
3
\$\begingroup\$

Stax, 9 bytes

1 is truthy, 0 is falsy

αAG<└\{┬⌠

Run and debug it

Explanation

|nX0-u%x:^=      # Full Program, unpacked, implicit input
|n               # Exponents of sequential primes in factorization. (eg. 20 -> [2 0 1])
  X              # Save to X register
   0-            # Remove all '0' from array
     u%          # Get unique numbers and get length of array
       x         # Copy back the array saved to X
        :^       # Is it ascending
         =       # Are the two comparisons equal? implicit output

Can probably be golfed more, but it covers the cases I was missing in the last solution.

\$\endgroup\$
3
\$\begingroup\$

MATL, 12 11 10 bytes

YFtgYsg)Zs

Try it at MATL Online!

Thanks to Luis Mendo for the remove-leading-zeroes part. He also pointed out that swapping truth values is allowed, so this returns 0 for numbers that satisfy the challenge requirements and any positive value otherwise.

Grosso Modo, this generates the exponents of the sequential prime factorization, removes leading zeroes and calculates the standard deviation.

\$\endgroup\$
  • \$\begingroup\$ I think 0iYFhdz works for 7 bytes: prepend a 0 to the exponents of the sequential factorization, consecutive differences, number of non-zeros. The result is 1 iff input satisfies the requirement \$\endgroup\$ – Luis Mendo May 18 '18 at 10:48
  • \$\begingroup\$ @LuisMendo Sorry for the delayed response, but you can post that as a separate answer. It is most definitely very different. \$\endgroup\$ – Mr. Xcoder May 20 '18 at 7:04
  • \$\begingroup\$ OK, I posted it as an answer \$\endgroup\$ – Luis Mendo May 20 '18 at 12:39
3
+100
\$\begingroup\$

Java 10, 223 191 178 176 168 bytes

n->{var s=new java.util.HashSet();for(int f=1,i=1,x,j;n>1;){for(x=++i,j=2;j<x;)x=x%j++<1?1:x;if(x>1){for(j=0;n%i<1&&n>(f=0);n/=i)j++;if(f<1)s.add(j);}}return s.size();}

Returns 1 as truthy, and >=2 as falsey.

Try it online.

Explanation:

n->{                   // Method with integer parameter and boolean return-type
  var s=new java.util.HashSet();
                       //  Set to keep track of the prime exponents
  for(int f=1,         //  Prime-flag, starting at 1
          i=1,x,j;     //  Index and temp integers
          n>1;){       //  Loop as long as `n` is still larger than 1
    for(x=++i,         //   Set `x` to `i`, after we've increased `i` by 1 first with `++i`
        j=2;           //   Set `j` to 2 (first prime)
        j<x;)          //   Inner loop as long as `j` is still smaller than `x`
      x=x%j++<1?       //    If `x` is divisible by `j`:
         1             //     Set `x` to 1
        :              //    Else:
         x;            //     Leave `x` unchanged
    if(x>1){           //    If `x` is larger than 1 (if `i` is a prime):
      for(j=0;         //     Use `j` as counter, and set it to 0
          n%i<1        //     If `n` is divisible by `i`:
                       //      And loop as long as `n` is still divisible by `i`,
          &&n>         //      and `n` is larger than 0
              (f=0);   //      (and set `f` to 0 at the same time)
          n/=i)        //       Divide `n` by `i`
        j++;           //       And increase `j` by 1
      if(f<1)          //     If the flag `f` is now/still 0:
        s.add(j);}}    //      Add counter `j` to the Set
  return s.size();}    //  Return the amount of items in the Set
                       //  (1 being true, >=2 being false)

Some example inputs:

n=15:

  • Flag remains 1 for the first prime 2 (because 15 is not divisible by 2).
  • Flag goes from 1 to 0 as soon as we're at the prime 3. Since 15 is divisible by 3, n becomes 5 (15/31), and the Set becomes [] → [1].
  • Then we check the next prime 5. Since 5 is divisible by 5, n becomes 1 (5/51), and the Set remains the same ([1] → [1]).
  • Now n=1, so we stop the outer loop. The Set ([1]) only contains one item, the 1 from both adjacent primes 3 and 5, so we return true.

n=14:

  • Flag goes from 1 to 0 for the first prime 2 (because 14 is divisible by 2). n becomes 7 (14/21), and the Set becomes [] → [1].
  • Then we check the next prime 3. Since 7 is not divisible by 3, n remains the same, and the Set becomes [1] → [1,0].
  • Then we check the next prime 5. Since 7 is also not divisible by 5, n remains the same, and the Set remains the same as well ([1,0] → [1,0]).
  • Then we check the next prime 7. Since 7 is divisible by 7, n becomes 1 (7/71), and the Set remains the same ([1,0] → [1,0]).
  • Now n=1, so we stop the outer loop. The Set ([1,0]) contains two items, the 1 from the non-adjacent primes 2 and 7, and the 0 from the primes 3 and 5, so we return false.

n=72:

  • Flag goes from 1 to 0 for the first prime 2, because 72 is divisible by 2 (multiple times). So n becomes 9 (72/23), and the Set becomes [] → [3].
  • Then we check the next prime 3. Since 9 is divisible by 3 (multiple times), n becomes 1 (9/32), and the Set becomes [3] → [3,2].
  • Now n=1, so we stop the outer loop. The Set ([3,2]) contains two items, the 3 from prime 2, and the 2 from prime 3, so we return false.
\$\endgroup\$
  • 1
    \$\begingroup\$ You can remove <2 and return an int (specify that you return 1 for truthy). \$\endgroup\$ – wastl May 20 '18 at 18:37
  • \$\begingroup\$ @wastl Ah, missed the rule about only one of the two values being consistent. In that case 1 is truthy and 2 or higher is falsey. Thanks. \$\endgroup\$ – Kevin Cruijssen May 20 '18 at 20:27
  • \$\begingroup\$ Thanks to whoever gave me the bounty, but why? \$\endgroup\$ – Kevin Cruijssen Feb 12 at 7:05
  • 1
    \$\begingroup\$ I had started a "Reward existing answer" bounty to bring more attention to my ECMAScript answer, which I still believe deserves more than it's gotten (I'd consider the bounty to have failed). When the week was over, I had to choose an answer other than my own to award the bounty to, or leave it to default to the highest-voted. I did not think any deserved it, but your answer had the best explanation, and that's why I awarded it to you; good explanations are way too rare on PPCG. As for my answer, I figure I have to give it a better writeup, which I plan on when I have time. \$\endgroup\$ – Deadcode Feb 15 at 20:35
  • 1
    \$\begingroup\$ @Deadcode Ah, so that's why. I thought maybe someone started the bounty, but they accidentally let it expire and it came to me instead. Still a bit confused why my answer then and not the highest voted one. I must say I'm impressed by all your Regex answers. I've seen some of them, and every time I'm amazed. Especially when I come back later to the same answer and you golfed it even loads more. :D I just noticed I hadn't seen nor upvoted the one for this challenge, so I just did. You know, I will add a bounty to this answer of yours. :) \$\endgroup\$ – Kevin Cruijssen Feb 15 at 21:16
2
\$\begingroup\$

J, 16 bytes

Big thanks to FrownyFrog for -8 bytes!

(=&#+/\=@#])_&q:

Try it online!

My old solution:

J, 24 bytes

[:(1=[:#@~.{.@I.}.])_&q:

Try it online!

Explanation:

_&q: prime exponents

{.@I.}.] removes the leading zeros by finding the first non-zero element:

     }.   drop
       ]  from the list of exponents
{.@       as much items as the first of the 
   I.     indices of non-zero elements

1=[:#@~. tests if all remaining numbers are equal:

  [:#@~.  finds the length of the list after removing the duplicates
1=        is it 1?
\$\endgroup\$
2
\$\begingroup\$

Husk, 11 bytes

Λ§*≈¤≈oṗ←gp

Try it online!

Outputs 0 if not a consecutive-prime/constant-exponent number.

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

MATL, 7 bytes

0iYFhdz

The result is 1 iff input satisfies the requirement.

Try it online! Or verify all test cases

Explanation

0    % Push 0
i    % Push input number
YF   % Exponents of consecutive prime factors
h    % Concatenate horizontally
d    % Consecutive differences
z    % Number of nonzeros. Implicitly display
\$\endgroup\$
2
\$\begingroup\$

Octave, 67 bytes

@(x)~any(diff(find(h=histc(factor(x),primes(x))))-1)&h(h>0)==max(h)

Try it online!

I believe this is the only solution that uses a histogram.

Explanation:

This makes a histogram, where the variable to be counted are the factors of the input, and placed in the bins primes(x), which is all primes less than the input. We then find the location of the prime factors, takes the difference between each of the indices and subtract one. If there are any elements that aren't zero (i.e. the difference of the indices of prime numbers is not 1), then this will result in a falsy value, otherwise it will return a truthy value.

We then chech that all non-zero elements in the histogram are equal to the maximum element. If there are values that aren't equal then this will result in a falsy value, otherwise it will return a truthy value.

If both those blocks are truthy then our input is a consecutive prime constant exponent number!

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

APL (Dyalog Extended), 28 bytes

{f p←`↓⍭⍵⋄(1=≢∪p)∧∨/f⍷⍸1⍭⍳⍵}

Try it online!

How:

{f p←`↓⍭⍵⋄(1=≢∪p)∧∨/f⍷⍸1⍭⍳⍵} ⍝ Monadic function, takes an argument ⍵
       ⍭⍵                     ⍝ Prime factors and exponents of ⍵
     `↓                        ⍝ split the resulting matrix in 2 vectors
 f p←                          ⍝ assign the factors to f and the powers to p
         ⋄                     ⍝ then
                          ⍳⍵   ⍝ range [1..⍵]
                        1⍭     ⍝ primality check for each element in the vector
                       ⍸        ⍝ where; returns the indices of truthy values
                     f⍷         ⍝ find the factors; returns a boolean vector
                   ∨/           ⍝ logical OR reduction
                  ∧             ⍝ logical AND
           (   ∪p)              ⍝ unique members of the powers
              ≢                 ⍝ tally; returns the number of elements in the vector
            1=                  ⍝ check if there's only one element
\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica), 65 bytes

Differences[{PrimePi@#,#2}&@@@FactorInteger@#]~MatchQ~{{1,0}...}&

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 63 bytes

n->#Set(vector(#(a=factor(n)~),i,[primepi(a[1,i])-i,a[2,i]]))<2

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 14 bytes

1#.2~:/\0,_&q:

1 in the output indicates consecutive constant exponent.

Try it online!

        0,_&q:   zero followed by the prime exponents of input
   2~:/\         for every two consecutive values, 1 if they are different
1#.              convert from base-1, just add them up
\$\endgroup\$
0
\$\begingroup\$

Clean, 127 bytes

import StdEnv
@n=[]== $n
?n#j= $n
= @n||j==filter@[hd j..last j]&&any(\p=(prod j)^p==n)[1..n]
$n=[i\\i<-[2..n-1]|n/i*i==n&& @i]

Try it online!

Defines the function ? :: Int -> Bool using $ :: Int -> [Int] to factorize and @ :: Int -> Bool to check primality.

\$\endgroup\$
0
\$\begingroup\$

APL(NARS) 41 chars, 82 bytes

{(1=≢∪+/¨{v=⍵⊃v}¨⍳≢v)∧(1↓w)≡¯1↓1πw←∪v←π⍵}

{π⍵} is the function factorization of argument ⍵ in the list of prime factors (repeat if one prime appear more time);
{1π⍵} is the function next prime (note that in this case its argument is not a scalar but one array of integers). test:

  h←{(1=≢∪+/¨{v=⍵⊃v}¨⍳≢v)∧(1↓w)≡¯1↓1πw←∪v←π⍵}
  (2..30)/⍨h¨2..30
2 3 4 5 6 7 8 9 11 13 15 16 17 19 23 25 27 29 30 
  h¨27000 456533 72 10000000
1 1 0 0 
\$\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.