# Climb a step to a prime

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 = 2^2 \cdot 3 \cdot 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(2^2 \cdot 3 \cdot 5) = 2235\$$.

(The above definition is taken from Problem 5 of Five $1,000 Problems - John H. Conway) Note that $$\f(13532385396179) = f(13 \cdot 53^2 \cdot 3853 \cdot 96179) = 13532385396179\$$. ## Task Take a positive composite integer $$\n\$$ as input, and output $$\f(n)\$$. ## Another example $$\48 = 2^4 \cdot 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  • +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! :) Commented Jun 9, 2017 at 10:21
• Without 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. Commented Jun 9, 2017 at 12:59

# Python, 166162 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)

• Why did someone downvote a newcomer instead of helping him improve his answer as @LeakyNun did? :( Commented Jun 9, 2017 at 11:30
• Sorry- 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
Commented Jun 9, 2017 at 11:33
• Welcome 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)
Commented Jun 9, 2017 at 11:47
• Oh my God, I didn't expect Jim Davis himself to appear in this site, and to answer my challenge... I feel so honoured now... Commented Jun 9, 2017 at 13:15
• ehh, not a troll by the way. My email address is on gladhoboexpress.blogspot.ca/2014/10/climb-to-prime.html ... I left the post up, nobody swamps you with email over math.
– jchd
Commented Jun 9, 2017 at 13:38

## 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.

• V = "concatenate to a single string and eval as Jelly" Commented Jun 9, 2017 at 10:48
• @EriktheOutgolfer Yes, hence "effectively". Commented Jun 9, 2017 at 10:49
• @MartinEnder Any particular reason you don't use Ḍ (Convert from decimal to integer)? Commented Jul 3, 2017 at 19:00
• @Christian Because the list might contain multi-digit integers. Commented Jul 3, 2017 at 19:26
• @MartinEnder Ah, clever. I've used FḌ in the past - that's a good tip! Commented Jul 4, 2017 at 0:18

# 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.

# Mathics, 34 bytes

Row[Join@@FactorInteger@#/.1->""]&


Try it online!

-2 bytes from @sanchez

• DeleteCases is long, you can use /.1->"" or /.1->##&[] (alternative form of /.1->Nothing Commented Jun 9, 2017 at 10:42
• @user202729 All of those need a space in front of the 1 to prevent it from parsing as ... / (0.1). Commented Jun 9, 2017 at 10:48
• You are right! fixed Commented Jun 9, 2017 at 14:40
• Using Join, i.e. Row[Join@@FactorInteger@#/.1->""]& results in 34 bytes Commented Feb 18, 2021 at 13:14

## 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.

• I would have used e_ to flatten, since that's what it's there for, but it doesn't change the score. Commented Jun 9, 2017 at 14:31
• @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. Commented Jun 9, 2017 at 14:36

# 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


# 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

• Fails for 48. Commented Jun 9, 2017 at 10:02

# 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


# Pyth, 11 bytes

jksm_-d1r8P


Try here

# 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!).

# 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



# 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


# 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!

# 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.

# APL (Dyalog Extended), 15 14 bytes

Saved 1 byte thanks to @Adám

∊⍕¨1~⍨∊⍉2⌂pco⎕


Try it online!

∊⍕¨1~⍨∊⍉2⌂pco⎕
⎕      ⍝ Input
2⌂pco        ⍝ Prime factors with exponents
⍉             ⍝ Transpose so the 1st column is factors, 2nd is exponents
∊               ⍝ Flatten
1~⍨               ⍝ Remove 1
⍕¨                   ⍝ Convert each to a string
∊                      ⍝ Concatenate them together


# 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));

}
}


# 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
)


# 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;
};

• 91 bytes Commented Apr 1, 2019 at 3:41

# 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.

# Ruby, 45 + 7 bytes

Requires the flag -rprime.

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


Try it online!

# Pyth - 16 bytes

V{PQpNK/PQNItKpK


Try it

Another solution:

smd-.nm(d/PQd){PQ1

• One can replace FN by V. Commented Jun 11, 2017 at 17:34
• Also, r8` (run-length encoding) seems to be useful. Commented Jun 11, 2017 at 17:35