Climb a step to a prime

24

The title of Numberphile's newest video, 13532385396179, is a fixed point of the following function f on the positive integers:

Let n be a positive integer. Write the prime factorization in the usual way, e.g. 60 = 22 · 3 · 5, in which the primes are written in increasing order, and exponents of 1 are omitted. Then bring exponents down to the line and omit all multiplication signs, obtaining a number f (n). [...] for example, f (60) = f (22 · 3 · 5) = 2235.

(The above definition is taken from Problem 5 of Five $1,000 Problems - John H. Conway)

Note that f (13532385396179) = f (13 · 532 · 3853 · 96179) = 13532385396179.

Task

Take a positive composite integer n as input, and output f(n).

Another example

48 = 24 · 3, so f (48) = 243.

Testcases

More testcases are available here.

   4 -> 22
   6 -> 23
   8 -> 23
  48 -> 243
  52 -> 2213
  60 -> 2235
 999 -> 3337
9999 -> 3211101

Leaky Nun

Posted 2017-06-09T09:48:01.160

Reputation: 45 011

11+1 I'm still astonished that someone managed to find 13532385396179 as a disproof of the conjecture. I guess the $1000 prize would go some way to pay for the electricity used! :) – Wossname – 2017-06-09T10:21:37.097

7Without following the link it wasn't clear that the conjecture is that repeated applications of f(n) will always reach a prime (and of course f(p) = p if p is prime). 13532385396179 disproves the conjecture because it's both composite and a fixed opint. – Chris H – 2017-06-09T12:59:22.283

Answers

16

Python, 166 162 159 bytes

You guys are much better. This is what I used! (the algorithm that solved it calls this)

from primefac import*
def c(n):
 x=factorint(n)
 a=''
 for i in range(len(x)):
  l=min(x.keys())
  a+=str(l)
  if x[l]>1:a+=str(x[l])
  x.pop(l)
 return int(a)

jchd

Posted 2017-06-09T09:48:01.160

Reputation: 161

1>

  • Please include the language and number of bytes. 2. Please attempt to golf down your program (reduce the number of bytes) by for example using single-letter variables. 3. Please include the import statements (factorint is obviously not a built-in function).
  • < – Leaky Nun – 2017-06-09T11:21:15.843

    2Why did someone downvote a newcomer instead of helping him improve his answer as @LeakyNun did? :( – Shaggy – 2017-06-09T11:30:18.003

    3Sorry- that really is what I used (I found the number). I just thought the crummy code would be funny. You can take it down. – jchd – 2017-06-09T11:33:46.423

    9Welcome on the site. It's really nice to have you sharing with us your solution. (for people who don't know, Jim Davis is the one who solved this problem in the first place). However, answers to challenges need to follow some rules. If you just follow the suggestions from @LeakyNun, then you answer will be valid. (maybe have a look at the other answers to see how they usually look like) – Dada – 2017-06-09T11:47:30.860

    I edited in a golfed version. I do not know Python, so if I introduced syntax errors, please let me know :) – Stephen – 2017-06-09T13:03:15.007

    Thanks Stephen! I didn't mean to be rude and couldn't figure out how to delete the post. – jchd – 2017-06-09T13:04:57.710

    4Oh my God, I didn't expect Jim Davis himself to appear in this site, and to answer my challenge... I feel so honoured now... – Leaky Nun – 2017-06-09T13:15:20.983

    @JimDavis No problem, honestly I think we were being rude. We have fairly high standards for what is "valid". – Stephen – 2017-06-09T13:16:59.060

    1No, not at all. I didn't bother to learn the syntax to post or anything. All these little six bit programs look like magic! And I appreciate you being nice to somebody who is a troll for all you know. – jchd – 2017-06-09T13:21:39.547

    2

    ehh, not a troll by the way. My email address is on http://gladhoboexpress.blogspot.ca/2014/10/climb-to-prime.html ... I left the post up, nobody swamps you with email over math.

    – jchd – 2017-06-09T13:38:48.643

    You can remove the space before the asterisk, the 0, in range (range(0, x) == range(x)), that saves 3 bytes, together. If this is Python 2, you can also replace calls to str with repr notation, e.g. `l`, which would save you another 6 bytes :) – L3viathan – 2017-06-09T14:56:20.983

    It's extremely cool that you've posted a program, but importing a non-standard module is stretching things a little. ;) OTOH, the really tiny answers on this site come from golfing languages that have all sorts of crazy functions built in... – PM 2Ring – 2017-06-10T11:19:35.083

    Would a=0 work to save a byte? – user41805 – 2017-06-10T11:51:07.040

    1And by the way, it is against the policy of PPCG to edit a golf into another author's post, so feel free to rollback the edits if you wish. – user41805 – 2017-06-10T12:03:45.440

    9

    Brachylog, 8 bytes

    ḋoọc;1xc
    

    Try it online!

    Explanation

    Example input: 60
    
    ḋ          Prime decomposition: [5,3,2,2]
     o         Order: [2,2,3,5]
      ọ        Occurences: [[2,2],[3,1],[5,1]]
       c       Concatenate: [2,2,3,1,5,1]
        ;1x    Execute 1s: [2,2,3,5]
           c   Concatenate: 2235
    

    You can use ℕ₂ˢ (select all integers greater than or equal to 2) instead of ;1x, which is probably more readable and more in the spirit of Brachylog.

    Fatalize

    Posted 2017-06-09T09:48:01.160

    Reputation: 32 976

    9

    Jelly, 6 bytes

    ÆFFḟ1V
    

    Try it online!

    Explanation

    ÆF      Get prime factorisation of input as prime-exponent pairs.
      F     Flatten.
       ḟ1   Remove 1s.
         V  Effectively flattens the list into a single integer.
    

    Martin Ender

    Posted 2017-06-09T09:48:01.160

    Reputation: 184 808

    V = "concatenate to a single string and eval as Jelly" – Erik the Outgolfer – 2017-06-09T10:48:02.473

    @EriktheOutgolfer Yes, hence "effectively". – Martin Ender – 2017-06-09T10:49:01.163

    @MartinEnder Any particular reason you don't use (Convert from decimal to integer)? – scatter – 2017-07-03T19:00:09.700

    @Christian Because the list might contain multi-digit integers. – Martin Ender – 2017-07-03T19:26:07.477

    @MartinEnder Ah, clever. I've used FḌ in the past - that's a good tip! – scatter – 2017-07-04T00:18:12.817

    5

    Mathematica, 43 36 Bytes

    Row@Flatten@FactorInteger@#/. 1->""&
    

    Try it online!

    J42161217

    Posted 2017-06-09T09:48:01.160

    Reputation: 15 931

    2DeleteCases is long, you can use /.1->"" or /.1->##&[] (alternative form of /.1->Nothing – user202729 – 2017-06-09T10:42:56.183

    3@user202729 All of those need a space in front of the 1 to prevent it from parsing as ... / (0.1). – Martin Ender – 2017-06-09T10:48:09.450

    You are right! fixed – J42161217 – 2017-06-09T14:40:50.233

    4

    CJam, 8 bytes

    limF:~1-
    

    Try it online!

    Explanation

    li  e# Read input and convert to integer.
    mF  e# Get prime factorisation as prime-exponent pairs.
    :~  e# Flatten.
    1-  e# Remove 1s.
        e# Implicitly print a flattened representation of the list.
    

    Martin Ender

    Posted 2017-06-09T09:48:01.160

    Reputation: 184 808

    I would have used e_ to flatten, since that's what it's there for, but it doesn't change the score. – Peter Taylor – 2017-06-09T14:31:08.793

    1@PeterTaylor Hm yeah, I can never decide which one to use, but tend to go with e_ for deep flatten only and use :~ whenever it's just a single level. – Martin Ender – 2017-06-09T14:36:41.540

    4

    05AB1E, 10 bytes

    Òγʒ¬?gDië?
    

    Try it online!

    Ò          # Push list of prime factors with duplicates
     γ         # Break into chunks of consecutive elements
      ʒ        # For each
       ¬?      #   Print the first element
         gD    #   Push the length of this chunk twice
           ië  #   If not 1
             ? #     Print the length
    

    Riley

    Posted 2017-06-09T09:48:01.160

    Reputation: 11 345

    3

    05AB1E, 12 11 bytes

    Òγvy¬sgD≠×J
    

    Try it online!

    Explanation

    Ò            # calculate prime factors with duplicates
     γ           # group consecutive equal elements
      vy         # for each group
        ¬        # get the head without popping
         sg      # push the length of the group
           D≠×   # repeat the length (length != 1) times
              J  # join
    

    Emigna

    Posted 2017-06-09T09:48:01.160

    Reputation: 50 798

    Fails for 48. – Leaky Nun – 2017-06-09T10:02:28.770

    2

    Pyth, 12 bytes

    smjk_>hddr8P
    

    Try it!

    alternative, 12 bytes

    smjk<_AdGr8P
    

    Try that!

    explanation

    smjk_>hddr8P
               PQ  # prime factorization (already in correct order) of the implicit input: [3, 3, 11, 101]
             r8    # length encode: [[2, 3], [1, 11], [1, 101]]
     m             # map over the length encoded list (lambda variable: d)
         >hdd      # take the d[0] last elements of d (so only the last for d[0]==1 and all else)
        _          # reverse that list
      jk           # join into a string
    s              # conatenate the list of strings
    

    KarlKastor

    Posted 2017-06-09T09:48:01.160

    Reputation: 2 352

    2

    Pyth, 11 bytes

    jksm_-d1r8P
    

    Try here

    Erik the Outgolfer

    Posted 2017-06-09T09:48:01.160

    Reputation: 38 134

    2

    Python 2, 99 bytes

    n=input()
    r=''
    p=2
    while~-n:
     e=0
     while n%p<1:e+=1;n/=p
     r+=str(p)*(e>0)+str(e)*(e>1);p+=1
    print r
    

    Try it online!

    If inputs are restricted to be below 2147483659, both str(...) may be replaced by `...` saving 6 bytes (this program will be very slow for numbers affected anyway!).

    Jonathan Allan

    Posted 2017-06-09T09:48:01.160

    Reputation: 67 804

    2

    Ohm, 11 bytes

    o:_]D2<?O;J
    

    Try it online!

    Explanation

    o:_]D2<?O;J
    o           # Push prime factors with powers from input (Format [[prime,power],...]
     :          # For each...
      _          # Push current element
       ]         # flatten
        D        # Duplicate power
         2<? ;   # Is the power smaller than 2?
            O     # Delete top of stacks
              J  # Join
    

    Datboi

    Posted 2017-06-09T09:48:01.160

    Reputation: 1 213

    1

    PHP, 88 bytes

    for($i=2;1<$a=&$argn;)$a%$i?$i++:$a/=$i+!++$r[$i];foreach($r as$k=>$v)echo$k,$v<2?"":$v;
    

    Try it online!

    Jörg Hülsermann

    Posted 2017-06-09T09:48:01.160

    Reputation: 13 026

    1

    Japt, 19 bytes

    k ó¥ ®¯1 pZlÃc fÉ q
    

    Test it online!

    Explanation

     k ó¥  ®   ¯  1 pZlà c fÉ  q
    Uk ó== mZ{Zs0,1 pZl} c f-1 q  // Ungolfed
                                  // Implicit: U = input number
    Uk                            // Break U into its prime factors.
       ó==                        // Group into runs of equal items.
           mZ{         }          // Map each item Z in this to
              Zs0,1               //   Z.slice(0, 1) (the array of the first item),
                    pZl           //   with Z.length added at the end.
                                  // This returns an array of prime-exponent pairs (Jelly's ÆF).
                         c        // Flatten.
                           f-1    // Filter to the items X where X - 1 is truthy (removes '1's).
                               q  // Join the resulting array into a single string.
                                  // Implicit: output result of last expression
    

    ETHproductions

    Posted 2017-06-09T09:48:01.160

    Reputation: 47 880

    0

    C#, 206 100 bytes

    n=>{var r="";for(int d=1,c;++d<=n;){c=0;while(n%d<1){c++;n/=d;}r+=c>0?d+(c>1?c+"":""):"";}return r;}
    

    Full/Formatted version:

    using System;
    
    class P
    {
        static void Main()
        {
            Func<int, string> func = n =>
            {
                var r = "";
                for (int d = 1, c; ++d <= n;)
                {
                    c = 0;
                    while (n % d < 1)
                    {
                        c++;
                        n /= d;
                    }
    
                    r += c > 0 ? d + (c > 1 ? c + "" : "") : "";
                }
    
                return r;
            };
    
            Console.WriteLine(func(4));
            Console.WriteLine(func(6));
            Console.WriteLine(func(8));
            Console.WriteLine(func(48));
            Console.WriteLine(func(52));
            Console.WriteLine(func(60));
            Console.WriteLine(func(999));
            Console.WriteLine(func(9999));
    
            Console.ReadLine();
        }
    }
    

    TheLethalCoder

    Posted 2017-06-09T09:48:01.160

    Reputation: 6 930

    0

    Javascript - 91 bytes

    (x,r='',i=1,n)=>{while(x>i++){for(n=0;x%i<1;n++)x/=i;r+=(n>0?i+'':'')+(n>1?n:'')}return r}
    

    Explanation

    (x,r='',i=1,n)=>(          // input x is the number to process, r, i, n are default values only
        while(x>i++){          // iterate over i until x
            for(n=0;x%i<1;n++) // iterate over n until i is not a factor of x
                x/=i;          // factor i out of x
            r+=(n>0?i+'':'')   // append i to r if n > 0
                +(n>1?n:'')    // append n to r if n > 1
                               // i+'' prevents adding i and n before appending to r
        }
        return r               // return r by comma-operator and arrow function syntax
    )
    

    asgallant

    Posted 2017-06-09T09:48:01.160

    Reputation: 309

    0

    Java 8, 103 chars

    Pretty straightforward solution.

    n->{String r="";int d=2,c;while(n>1){c=0;while(n%d<1){c++;n/=d;}if(c>0)r+=d;if(c>1)r+=c;d++;}return r;}
    

    Ungolfed:

    private static Function<Integer, String> f = n->{
        String result = "";
        int divisor = 2, count;
        while (n>1) {
            count = 0;
            while (n % divisor < 1) {
                count++;
                n /= divisor;
            }
            if (count > 0) result += divisor;
            if (count > 1) result += count;
            divisor++;
        }
        return result;
    };
    

    Computronium

    Posted 2017-06-09T09:48:01.160

    Reputation: 151

    91 bytes – ceilingcat – 2019-04-01T03:41:42.423

    0

    Octave, 69 bytes

    @(a)printf('%d',(f=[[~,c]=hist(b=factor(a),d=unique(b));d](:))(f~=1))
    

    Try it online!

    Ended up being quite long, but this will generate the desired output.

    Essentially we use the histogram function to count the number of occurrences of the unique values in the prime factorisation of the input value.

    • The result of the factor() function gives the prime factors in ascending order
    • we then find unique() values in that array
    • hist() returns the number of occurrences

    Once we have the two arrays (one for unique factors, one for counts), we concatenate the arrays vertically (one on top of the other), and then flatten. This interleaves the factors with counts.

    Finally we display the result as a string ensuring to skip any 1's in the final array. The only time 1's can appear is if the count was 1 because 1 will never be a prime factor. This elimination is done before converting to a string so it won't affect things like the number 10.

    Tom Carpenter

    Posted 2017-06-09T09:48:01.160

    Reputation: 3 990

    0

    Ruby, 45 + 7 bytes

    Requires the flag -rprime.

    ->n{(n.prime_division.flatten-[1]).join.to_i}
    

    Try it online!

    canhascodez

    Posted 2017-06-09T09:48:01.160

    Reputation: 201

    0

    Pyth - 16 bytes

    V{PQpNK/PQNItKpK
    

    Try it

    Another solution:

    sm`d-.nm(d/PQd){PQ1
    

    Maria

    Posted 2017-06-09T09:48:01.160

    Reputation: 644

    1One can replace FN by V. – Leaky Nun – 2017-06-11T17:34:45.787

    1Also, r8 (run-length encoding) seems to be useful. – Leaky Nun – 2017-06-11T17:35:09.350

    0

    R, 72 bytes

    x=rle(pracma::factors(scan()));x$l[x$l<2]='';paste0(x$v,x$l,collapse='')
    

    Requires the pracma package, which is not installed on TIO.

    Giuseppe

    Posted 2017-06-09T09:48:01.160

    Reputation: 21 077