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
-
11\$\begingroup\$ +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! :) \$\endgroup\$Wossname– Wossname2017年06月09日 10:21:37 +00:00Commented Jun 9, 2017 at 10:21
-
8\$\begingroup\$ 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. \$\endgroup\$Chris H– Chris H2017年06月09日 12:59:22 +00:00Commented Jun 9, 2017 at 12:59
21 Answers 21
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)
-
2\$\begingroup\$ Why did someone downvote a newcomer instead of helping him improve his answer as @LeakyNun did? :( \$\endgroup\$Shaggy– Shaggy2017年06月09日 11:30:18 +00:00Commented Jun 9, 2017 at 11:30
-
3\$\begingroup\$ 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. \$\endgroup\$jchd– jchd2017年06月09日 11:33:46 +00:00Commented Jun 9, 2017 at 11:33
-
9\$\begingroup\$ 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) \$\endgroup\$Dada– Dada2017年06月09日 11:47:30 +00:00Commented Jun 9, 2017 at 11:47
-
4\$\begingroup\$ 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... \$\endgroup\$Leaky Nun– Leaky Nun2017年06月09日 13:15:20 +00:00Commented Jun 9, 2017 at 13:15
-
2\$\begingroup\$ 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. \$\endgroup\$jchd– jchd2017年06月09日 13:38:48 +00:00Commented Jun 9, 2017 at 13:38
Jelly, 6 bytes
ÆFFḟ1V
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.
-
\$\begingroup\$
V= "concatenate to a single string and eval as Jelly" \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年06月09日 10:48:02 +00:00Commented Jun 9, 2017 at 10:48 -
\$\begingroup\$ @EriktheOutgolfer Yes, hence "effectively". \$\endgroup\$Martin Ender– Martin Ender2017年06月09日 10:49:01 +00:00Commented Jun 9, 2017 at 10:49
-
\$\begingroup\$ @MartinEnder Any particular reason you don't use
Ḍ(Convert from decimal to integer)? \$\endgroup\$Christian Legge– Christian Legge2017年07月03日 19:00:09 +00:00Commented Jul 3, 2017 at 19:00 -
\$\begingroup\$ @Christian Because the list might contain multi-digit integers. \$\endgroup\$Martin Ender– Martin Ender2017年07月03日 19:26:07 +00:00Commented Jul 3, 2017 at 19:26
-
\$\begingroup\$ @MartinEnder Ah, clever. I've used
FḌin the past - that's a good tip! \$\endgroup\$Christian Legge– Christian Legge2017年07月04日 00:18:12 +00:00Commented Jul 4, 2017 at 0:18
Brachylog, 8 bytes
ḋoọc;1xc
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 N2s (select all integers greater than or equal to 2) instead of ;1x, which is probably more readable and more in the spirit of Brachylog.
-
2\$\begingroup\$
DeleteCasesis long, you can use/.1->""or/.1->##&[](alternative form of/.1->Nothing\$\endgroup\$user202729– user2027292017年06月09日 10:42:56 +00:00Commented Jun 9, 2017 at 10:42 -
3\$\begingroup\$ @user202729 All of those need a space in front of the
1to prevent it from parsing as... / (0.1). \$\endgroup\$Martin Ender– Martin Ender2017年06月09日 10:48:09 +00:00Commented Jun 9, 2017 at 10:48 -
\$\begingroup\$ You are right! fixed \$\endgroup\$ZaMoC– ZaMoC2017年06月09日 14:40:50 +00:00Commented Jun 9, 2017 at 14:40
-
\$\begingroup\$ Using
Join, i.e.Row[Join@@FactorInteger@#/.1->""]&results in 34 bytes \$\endgroup\$sanchez– sanchez2021年02月18日 13:14:06 +00:00Commented Feb 18, 2021 at 13:14
CJam, 8 bytes
limF:~1-
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.
-
\$\begingroup\$ I would have used
e_to flatten, since that's what it's there for, but it doesn't change the score. \$\endgroup\$Peter Taylor– Peter Taylor2017年06月09日 14:31:08 +00:00Commented Jun 9, 2017 at 14:31 -
1\$\begingroup\$ @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. \$\endgroup\$Martin Ender– Martin Ender2017年06月09日 14:36:41 +00:00Commented Jun 9, 2017 at 14:36
05AB1E, 10 bytes
Òγʒ¬?gDië?
Ò # 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¬sg×ばつJ
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
×ばつ # repeat the length (length != 1) times
J # join
-
\$\begingroup\$ Fails for
48. \$\endgroup\$Leaky Nun– Leaky Nun2017年06月09日 10:02:28 +00:00Commented Jun 9, 2017 at 10:02
Pyth, 12 bytes
smjk_>hddr8P
alternative, 12 bytes
smjk<_AdGr8P
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
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
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
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
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;
APL (Dyalog Extended), (削除) 15 (削除ここまで) 14 bytes
Saved 1 byte thanks to @Adám
∊⍕ ̈1~⍨∊⍉2⌂pco⎕
∊⍕ ̈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));
Console.ReadLine();
}
}
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;
};
-
\$\begingroup\$ 91 bytes \$\endgroup\$ceilingcat– ceilingcat2019年04月01日 03:41:42 +00:00Commented Apr 1, 2019 at 3:41
Octave, 69 bytes
@(a)printf('%d',(f=[[~,c]=hist(b=factor(a),d=unique(b));d](:))(f~=1))
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.
-
1\$\begingroup\$ One can replace
FNbyV. \$\endgroup\$Leaky Nun– Leaky Nun2017年06月11日 17:34:45 +00:00Commented Jun 11, 2017 at 17:34 -
1\$\begingroup\$ Also,
r8(run-length encoding) seems to be useful. \$\endgroup\$Leaky Nun– Leaky Nun2017年06月11日 17:35:09 +00:00Commented Jun 11, 2017 at 17:35