Inspired by this question over at Mathematics.
The Problem
Let
nbe a natural number≥ 2. Take the biggest divisor ofn– which is different fromnitself – and subtract it fromn. Repeat until you get1.
The Question
How many steps does it take to reach 1 for a given number n ≥ 2.
Detailed Example
Let
n = 30.
The greatest divisor of:
1. 30 is 15 --> 30 - 15 = 15
2. 15 is 5 --> 15 - 5 = 10
3. 10 is 5 --> 10 - 5 = 5
4. 5 is 1 --> 5 - 1 = 4
5. 4 is 2 --> 4 - 2 = 2
6. 2 is 1 --> 2 - 1 = 1
It takes 6 steps to reach 1.
Input
- Input is an integer
n, wheren ≥ 2. - Your program should support input up to the language's maximum integer value.
Output
- Simply output the number of steps, like
6. - Leading/trailing whitespaces or newlines are fine.
Examples
f(5) --> 3
f(30) --> 6
f(31) --> 7
f(32) --> 5
f(100) --> 8
f(200) --> 9
f(2016^155) --> 2015
Requirements
- You can get input from
STDIN, command line arguments, as function parameters or from the closest equivalent. - You can write a program or a function. If it is an anonymous function, please include an example of how to invoke it.
- This is code-golf so shortest answer in bytes wins.
- Standard loopholes are disallowed.
This series can be found on OEIS as well: A064097
A quasi-logarithm defined inductively by
a(1) = 0anda(p) = 1 + a(p-1)ifpis prime anda(n*m) = a(n) + a(m)ifm,n > 1.
49 Answers 49
Jelly, 9 bytes
ÆṪÐL·ÆFL€S
Try it online! or verify all test cases.
Background
The definition of sequence A064097 implies that
$$\small{a(p)-a(p-1)=1}\text{ for all primes }p\text{ and }a(km)=a(k)+a(m)\text{ for all }k,m\in \mathbb{N}$$
$$\small{\varphi(n)=n\prod_{p|n}\left(1-{1\over p}\right)=n\prod_{p|n}{{p-1}\over p}}$$
where \$\varphi\$ denotes Euler's totient function and \$p\$ varies only over prime numbers.
Combining both, we deduce the property
$$\small{a(\varphi(n))=a\left(n\prod_{p|n}{{p-1}\over p}\right)=a(n)+\sum_{p|n}\left(a(p-1)-a(p)\right)=a(n)-\sum_{p|n}{1=a(n)-\omega(n)}}$$
where \$\omega\$ denotes the number of distinct prime factors of \$n\$.
Applying the resulting formula \$k + 1\$ times, where \$k\$ is large enough so that \$\varphi^{k+1}(n)=1\$, we get
$$\small{ \begin{aligned} 0=a\left(\varphi^{k+1}(n)\right)=a\left(\varphi^k(n)\right)-\omega\left(\varphi^k(n)\right)&=a\left(\varphi^{k-1}(n)\right)-\omega\left(\varphi^{k-1}(n)\right)-\omega\left(\varphi^k(n)\right) \\ &= \cdots = a(n)-\sum_{j=0}^k\omega\left(\varphi^j(n)\right) \end{aligned} }$$
From this property, we obtain the formula
$$\small{a(n)=\sum_{j=0}^k\omega\left(\varphi^j(n)\right)=\sum_{j=0}^{+\infty}\omega\left(\varphi^j(n)\right)}$$
where the last equality holds because \$\omega(1)=0\$.
How it works
ÆṪÐL·ÆFL€S Main link. Argument: n
ÐL· Repeatedly apply the link to the left until the results are no longer
unique, and return the list of unique results.
ÆṪ Apply Euler's totient function.
Since φ(1) = 1, This computes φ-towers until 1 is reached.
ÆF Break each resulting integer into [prime, exponent] pairs.
L€ Compute the length of each list.
This counts the number of distinct prime factors.
S Add the results.
-
1\$\begingroup\$ Now that is a super clever approach ! \$\endgroup\$Abr001am– Abr001am2016年05月13日 15:59:34 +00:00Commented May 13, 2016 at 15:59
-
1\$\begingroup\$ With the latest version of Jelly, this is 7 bytes, meaning it's still in (joint) first \$\endgroup\$2020年11月02日 16:42:24 +00:00Commented Nov 2, 2020 at 16:42
05AB1E, (削除) 13 (削除ここまで) 11 bytes
Code:
[DÒ¦P-1⁄4D#]3⁄4
Explanation:
[ ] # An infinite loop and...
D# break out of the loop when the value is equal to 1.
D # Duplicate top of the stack (or in the beginning: duplicate input).
Ò # Get the prime factors, in the form [2, 3, 5]
¦ # Remove the first prime factor (the smallest one), in order to get
the largest product.
P # Take the product, [3, 5] -> 15, [] -> 1.
- # Substract from the current value.
1⁄4 # Add one to the counting variable.
3⁄4 # Push the counting variable and implicitly print that value.
Uses CP-1252 encoding. Try it online!.
-
15\$\begingroup\$ Remove the first prime factor (the smallest one), in order to get the largest product How clever! :-) \$\endgroup\$Luis Mendo– Luis Mendo2016年05月09日 13:58:03 +00:00Commented May 9, 2016 at 13:58
-
\$\begingroup\$ I see, you are the language developer \$\endgroup\$Display Name– Display Name2016年05月11日 12:56:51 +00:00Commented May 11, 2016 at 12:56
-
\$\begingroup\$ @SargeBorsch Yes, that is correct :) \$\endgroup\$Adnan– Adnan2016年05月11日 16:10:21 +00:00Commented May 11, 2016 at 16:10
-
\$\begingroup\$
[¼Ñü-¤ÄD#]¾- I was close to shaving off a byte with pairwise, oh well... \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年01月18日 18:57:08 +00:00Commented Jan 18, 2017 at 18:57 -
1\$\begingroup\$ Down to 9:
[Ð#Ò¦P-]N. You don't need the counter variable. \$\endgroup\$Grimmy– Grimmy2019年05月22日 13:19:14 +00:00Commented May 22, 2019 at 13:19
Pyth, 11 bytes
fq1=-Q/QhPQ
A straightforward repeat-until-true loop.
Explanation:
fq1=-Q/QhPQ
Implicit: Q = eval(input())
f Apply the following function until it is truthy,
incrementing T each time starting at 1:
PQ Take the prime factorization of Q
h Take its first element, the smallest factor of Q
/Q Divide Q by that, giving Q's largest factor
-Q Subtract the result from Q
= Assign Q to that value
q1 Check if Q is now 1.
-
\$\begingroup\$ that's a really nice trick with
filter. \$\endgroup\$Maltysen– Maltysen2016年05月09日 20:49:39 +00:00Commented May 9, 2016 at 20:49 -
3\$\begingroup\$ I don't understand why this outputs the number of times the function ran. Is this an undocumented feature of
f? \$\endgroup\$corsiKa– corsiKa2016年05月10日 20:35:04 +00:00Commented May 10, 2016 at 20:35 -
\$\begingroup\$ @corsiKa
fwithout a second argument iterates over all positive integers starting from1and returns the first value that gives true on the inner statement. This value happens to be unused in this program, so it returns the number of times that it ran. Not undocumented, just unorthodox :) If it helps, you can think of this as aforloop like:for(int i=1; some_condition_unrelated_to_i; i++) { change_stuff_that_affects_condition_but_not_i;}\$\endgroup\$FryAmTheEggman– FryAmTheEggman2016年05月13日 15:30:26 +00:00Commented May 13, 2016 at 15:30 -
\$\begingroup\$ @corsiKa It's documented in the character reference on the right side of the online interpreter. With only one argument (
f <l:T> <none>),fis First input whereA(_)is truthy over[1, 2, 3, 4...]. \$\endgroup\$Dennis– Dennis2016年05月13日 15:31:08 +00:00Commented May 13, 2016 at 15:31 -
\$\begingroup\$ Ah I understand it now. It uses that input but never uses the input in the calculation. That explains @Maltysen comment of "that's a really nice trick" because you only care about the iteration count not using that count anywhere in your filter. I love those ah-ha moments!: ) \$\endgroup\$corsiKa– corsiKa2016年05月13日 15:48:09 +00:00Commented May 13, 2016 at 15:48
Retina, 12
- 14 bytes saved thanks to @MartinBüttner
(1+)(?=1円+$)
This assumes input given in unary and output given in decimal. If this is not acceptable then we can do this for 6 bytes more:
Retina, 18
- 8 bytes saved thanks to @MartinBüttner
.+ $* (1+)(?=1円+$)
Try it online - 1st line added to run all testcases in one go.
Sadly this uses unary for the calculations, so input of 2016155 is not practical.
- The first stage (2 lines) simply converts the decimal input to unary as a string of
1s - The second stage (1 line) calculates the largest factor of n using regex matching groups and lookbehinds to and effectively subtracts it from n. This regex will match as many times as is necessary to reduce the number as far as possible. The number of regex matches will be the number of steps, and is output by this stage.
-
\$\begingroup\$ I don't think you need the
\b. \$\endgroup\$Martin Ender– Martin Ender2016年05月09日 20:31:22 +00:00Commented May 9, 2016 at 20:31 -
\$\begingroup\$ You can save a lot more like this though and technically you don't need the first stage either. \$\endgroup\$Martin Ender– Martin Ender2016年05月09日 20:36:25 +00:00Commented May 9, 2016 at 20:36
Python 2, (削除) 50 (削除ここまで) 49 bytes
f=lambda n,k=1:2/n or n%(n-k)and f(n,k+1)or-~f(k)
This isn't going to finish that last test case any time soon...
Alternatively, here's a 48-byte which returns True instead of 1 for n=2:
f=lambda n,k=1:n<3or n%(n-k)and f(n,k+1)or-~f(k)
Jelly, 10 bytes
ÆfḊPạμÐL·i2
Try it online! or verify most test cases. The last test cases finishes quickly locally.
How it works
ÆfḊPạμÐL·i2 Main link. Argument: n (integer)
Æf Factorize n, yielding a list of primes, [] for 1, or [0] for 0.
Ḋ Dequeue; remove the first (smallest) element.
P Take the product.
This yields the largest proper divisor if n > 1, 1 if n < 2.
ạ Yield the abs. value of the difference of the divisor (or 1) and n.
μ Convert the chain to the left into a link.
ÐL· Repeatedly execute the link until the results are no longer unique.
Collect all intermediate results in a list.
For each starting value of n, the last results are 2 -> 1 -> 0 (-> 1).
i2 Compute the 1-based index of 2.
Pyth - (削除) 15 (削除ここまで) (削除) 14 (削除ここまで) 13 bytes
Special casing 1 is really killing me.
tl.u-N/Nh+PN2
tl One minus the length of
.u Cumulative fixed point operator implicitly on input
-N N -
/N N /
h Smallest prime factor
+PN2 Prime factorization of lambda var, with two added to work with 1
-
1\$\begingroup\$ One thing I always forget.... brute force is often the golfiest approach \$\endgroup\$Leaky Nun– Leaky Nun2016年05月09日 13:34:29 +00:00Commented May 9, 2016 at 13:34
-
\$\begingroup\$ What do you mean with special casing
1? \$\endgroup\$Adnan– Adnan2016年05月09日 13:40:40 +00:00Commented May 9, 2016 at 13:40 -
1\$\begingroup\$ @Adnan the prime factorization of
1is[], which causes an error when I take the first element. I have to special case it to make it return1again so the.ufixed-point ends. I found a better way than.xtry-except which is what saved me those 2 bytes. \$\endgroup\$Maltysen– Maltysen2016年05月09日 13:41:51 +00:00Commented May 9, 2016 at 13:41 -
\$\begingroup\$ It only needs to accept numbers >= 2 (>1). \$\endgroup\$Solomon Ucko– Solomon Ucko2016年05月09日 22:49:00 +00:00Commented May 9, 2016 at 22:49
-
\$\begingroup\$ @SolomonUcko you're misunderstanding, the
.ufixed-point will eventually reach1for all input, at which point it will have to be special cased. \$\endgroup\$Maltysen– Maltysen2016年05月09日 22:53:10 +00:00Commented May 9, 2016 at 22:53
Julia, (削除) 56 (削除ここまで) (削除) 50 (削除ここまで) (削除) 45 (削除ここまで) 39 bytes
f(n)=n>1&&f(n-n÷first(factor(n))[1])+1
This is a recursive function that accepts an integer and returns an integer.
Ungolfed:
function f(n)
if n < 2
# No decrementing necessary
return 0
else
# As Dennis showed in his Jelly answer, we don't need to
# divide by the smallest prime factor; any prime factor
# will do. Since `factor` returns a `Dict` which isn't
# sorted, `first` doesn't always get the smallest, and
# that's okay.
return f(n - n ÷ first(factor(n))[1]) + 1
end
end
Try it online! (includes all test cases)
Saved 6 bytes thanks to Martin Büttner and 11 thanks to Dennis!
JavaScript (ES6), (削除) *44 (削除ここまで) 38
Edit 6 bytes saved thanks @l4m2
(* 4 striked is still 4)
Recursive function
f=(n,d=n)=>n>1?n%--d?f(n,d):f(n-d)+1:0
Less golfed
f=(n, d=n-1)=>{
if (n>1)
if(n % d != 0)
return f(n, d-1) // same number, try a smaller divisor
else
return f(n-d)+1 // reduce number, increment step, repeat
else
return 0
}
Test
f=(n,d=n)=>n>1?n%--d?f(n,d):f(n-d)+1:0
console.log=x=>O.textContent+=x+'\n';
[5,30,31,32,100,200].forEach(x=>console.log(x+' -> '+f(x)))
<pre id=O></pre>
-
\$\begingroup\$ Nice, but I think you should spend the two bytes needed to make f(1) == 0. \$\endgroup\$Neil– Neil2016年05月11日 11:39:34 +00:00Commented May 11, 2016 at 11:39
-
\$\begingroup\$ @Neil thinking again: no. "Let n be a natural number ≥ 2 ..." \$\endgroup\$edc65– edc652016年05月11日 14:18:16 +00:00Commented May 11, 2016 at 14:18
-
\$\begingroup\$ I need new glasses. \$\endgroup\$Neil– Neil2016年05月12日 13:44:41 +00:00Commented May 12, 2016 at 13:44
-
\$\begingroup\$ Why not
f=(n,d=n)=>n>1?n%--d?f(n,d):f(n-d)+1:0? \$\endgroup\$l4m2– l4m22018年04月13日 03:50:23 +00:00Commented Apr 13, 2018 at 3:50 -
\$\begingroup\$ @l4m2 right, why not? Thanks \$\endgroup\$edc65– edc652018年04月13日 14:55:17 +00:00Commented Apr 13, 2018 at 14:55
Regex 🐝 (ECMAScript or better), 12 bytes
(x+)(?=1円+$)
Takes its input in unary, as a sequence of x characters whose length represents the number. Returns its output as the number of matches.
This of course has already been done in Digital Trauma's Retina answer, but here it is demonstrated to work on a much wider variety of regex engines.
Try it online! - ECMAScript
Try it online! - Perl
Try it online! - Java
Try it online! - PCRE
Try it online! - Boost
Try it online! - Python
Try it online! - Ruby
Try it online! - .NET
(x+)(?=2円+$) # 2円 = largest proper divisor of tail; tail -= 2円;
# return 2円 as a match
Regex 🐘 (.NET), 15 bytes
((x+)(?=2円+$))*
Returns its output as the capture count of group 1円.
# tail = input number; no need to anchor, as all inputs return a result
(
(x+)(?=2円+$) # 2円 = largest proper divisor of tail; tail -= 2円
)* # Loop the above as many times as possible, minimum zero, and push each
# match onto the 1円 capture stack
Regex (Perl / Java / PCRE2 v10.34 or later), 26 bytes
((?=(2円?+(x*))x(x3円)+$)x)*
Returns its output as the length of the match.
Try it online! - Perl
Try it online! - Java
Attempt This Online! - PCRE2 v10.40+
We can't just do a direct port of the .NET version, because we need to subtract at least 1 from tail on each iteration (as a regex loop terminates after a zero-width match), and with most inputs, the iteration count exceeds the cumulative result of subtracting the largest divisor before the last iteration has finished. So instead, we let 2円 be the sum of the subtracted divisors minus the iteration count:
# tail = input number; no need to anchor, as all inputs return a result
(
(?=
(2円?+(x*)) # 3円 = {conjectured largest proper divisor of tail-2円} - 1;
# 2円 = {2,円 or 0 if 2円 is unset} + 3円; tail -= 2円; note that the
# subtraction of 1 from this divisor compensates for the "tail -= 1"
# on each iteration (below)
x(x3円)+$ # assert tail - 1 is divisible by 3円 + 1
)
x # head += 1; tail -=1
)* # Loop the above as many times as possible, minimum zero
(The PCRE version requirement is because PCRE2 v10.33 and earlier automatically makes any group containing a nested backreference atomic, thus 3円 always captures the entire remaining tail, making the regex always return 0.)
Regex (.NET), 28 bytes
(?=((x+)(?=2円+$))*)(?<-1>x)*
A simple port of the 15 byte .NET version, that returns its output as the length of the match instead.
Regex (Perl / Java / PCRE2 v10.34 or later / .NET), (削除) 29 (削除ここまで) bytes
Obsoleted by the 29 byte regex below, which supports a superset of regex engines.
Regex (Perl / Java / PCRE / Pythonregex / Ruby / .NET), (削除) 33 (削除ここまで) 29 bytes
((?=.*(?=3円$|^)(x+)(2円+$))x)*
Try it online! - Perl
Try it online! - Java
Try it online! - PCRE1
Try it online! - PCRE2 v10.33
Attempt This Online! - PCRE2 v10.40+
Try it online! - Python import regex
Try it online! - Ruby
Try it online! - .NET
For regex engines that lack nested backreferences but support forward-declared backreferences, we switch to a different approach. In this version, each iteration, while inside a lookahead, stores the new value of tail in the capture group 3円, so that the next iteration can then recall that value rather directly. Since this directly sets 3円 instead of building it up incrementally, there's no need to emulate a nested backreference.
# tail = N = input number;
# no need to anchor, as all inputs return a result
(
(?=
.*(?=3円$|^) # tail = 3,円 or N if 3円 is unset
(x+)(2円+$) # 2円 = largest proper divisor of tail; 3円 = tail - 2円
)
x # Increment the return value
)* # Loop the above as many times as possible, minimum zero
Regex (Perl / PCRE / Pythonregex ), 63 bytes
(?=(xx+?)1円*(?=1円$)((?R)))(?=(x+)3円*(?=3円$)((?R)))2円4円|x\B(?R)|
This implements the recursive definition of the function:
-
\$a(1) = 0\$
\$a(p) = 1 + a(p-1)\$ if \$p\$ is prime
\$a(nm) = a(n) + a(m)\$ if \$m,n > 1\$
Try it online! - Perl
Try it online! - PCRE1
Try it online! - PCRE2 v10.33
Attempt This Online! - PCRE2 v10.40+
Try it online! - Python import regex
# tail = input number; no need to anchor, as all inputs return a result
(?=
(xx+?)1円*(?=1円$) # Assert tail isn't prime;
# tail = 1円 = smallest prime factor of tail
((?R)) # 2円 = recursive result of f(tail)
)
(?=
(x+)3円*(?=3円$) # tail = 3円 = largest proper divisor of tail
((?R)) # 4円 = recursive result of f(tail)
)
2円4円 # return 2円 + 4円 as the match
| # or if tail is prime:
x # tail -= 1; return result += 1
\B # Assert tail > 0
(?R) # return 1 + recursive result of f(tail)
| # or if tail == 1:
# return 0 at the match
Note that (?0) could have been used as a synonym for (?R).
Regex (Perl / PCRE / Boost / Pythonregex ), 65 bytes
((?=(xx+?)2円*(?=2円$)((?1)))(?=(x+)4円*(?=4円$)((?1)))3円5円|x\B(?1)|)
The intent of the 63 byte version was to create a version that works under Boost, but as it turned out Boost has a bug (which I've reported) in which it ignores top-level alternatives in a top-level recursive call (i.e. (?R)), trying only the first top-level alternative. The workaround is to nest the entire regex in a capture group, so that Boost sees the other alternatives. Either (?R) or (?1) would work, but the latter is used for slightly better efficiency.
Try it online! - Perl
Try it online! - PCRE2
Try it online! - Boost
Try it online! - Python import regex
Regex (Ruby), (削除) 76 (削除ここまで) 71 bytes
(?=(xx+?)1円*(?=1円$)(\g<0>))(?=(x+)3円*(?=3円$)(\g<0>))\k<2+0>4円|x\B\g<0>|
This is a port to Ruby's style of recursion; (?R) is replaced with \g<0>. Also, the backreference 2円 needs to be replaced with \k<2+0> to get its value at the current level of recursion, rather than its global value which may have been overwritten at deeper levels of recursion.
-
1\$\begingroup\$ That’s awesome. :) \$\endgroup\$insertusernamehere– insertusernamehere2022年07月12日 17:06:21 +00:00Commented Jul 12, 2022 at 17:06
Mathematica, 36 bytes
f@1=0;f@n_:=f[n-Divisors[n][[-2]]]+1
An unnamed function takes the same bytes:
If[#<2,0,#0[#-Divisors[#][[-2]]]+1]&
This is a very straightforward implementation of the definition as a recursive function.
PowerShell v2+, 81 bytes
param($a)for(;$a-gt1){for($i=$a-1;$i-gt0;$i--){if(!($a%$i)){$j++;$a-=$i;$i=0}}}$j
Brutest of brute force.
Takes input $a, enters a for loop until $a is less than or equal to 1. Each loop we go through another for loop that counts down from $a until we find a divisor (!($a%$i). At worst, we'll find $i=1 as a divisor. When we do, increment our counter $j, subtract our divisor $a-=$i and set $i=0 to break out of the inner loop. Eventually, we'll reach a condition where the outer loop is false (i.e., $a has reached 1), so output $j and exit.
Caution: This will take a long time on larger numbers, especially primes. Input of 100,000,000 takes ~35 seconds on my Core i5 laptop. Edit -- just tested with [int]::MaxValue (2^32-1), and it took ~27 minutes. Not too bad, I suppose.
Octave, (削除) 59 (削除ここまで) (削除) 58 (削除ここまで) 55 bytes
function r=f(x)r=0;while(x-=x/factor(x)(1));r++;end;end
Updated thanks to Stewie Griffin, saving 1 byte
Further update, saving three more bytes by using result of factorization in while-check.
Sample runs:
octave:41> f(5)
ans = 3
octave:42> f(30)
ans = 6
octave:43> f(31)
ans = 7
octave:44> f(32)
ans = 5
octave:45> f(100)
ans = 8
octave:46> f(200)
ans = 9
-
\$\begingroup\$ is the last
endnecessary in octave ? \$\endgroup\$Abr001am– Abr001am2016年05月09日 20:36:26 +00:00Commented May 9, 2016 at 20:36 -
\$\begingroup\$ It is. I noticed it was not in matlab from your answers, but Octave expects it (as I learned from trying yours in Octave). \$\endgroup\$dcsohl– dcsohl2016年05月09日 20:43:56 +00:00Commented May 9, 2016 at 20:43
Haskell, 59 bytes
f 1=0;f n=1+(f$n-(last$filter(\x->n`mod`x==0)[1..n`div`2]))
Usage:
Prelude> f 30
Prelude> 6
It may be a little inefficient for big numbers because of generating the list.
-
1\$\begingroup\$ List comprehension and
<1instead of==0saves a few bytes:f 1=0;f n=1+f(n-last[a|a<-[1..ndiv2],mod n a<1])\$\endgroup\$Angs– Angs2018年04月13日 15:25:50 +00:00Commented Apr 13, 2018 at 15:25 -
1\$\begingroup\$ You can save a character by rewriting
(f$...)tof(...). \$\endgroup\$Lemming– Lemming2020年04月13日 08:08:44 +00:00Commented Apr 13, 2020 at 8:08
Python 3, (削除) 75, 70 (削除ここまで), 67 bytes.
g=lambda x,y=0:y*(x<2)or[g(x-z,y+1)for z in range(1,x)if x%z<1][-1]
This is a pretty straight forward recursive solution. It takes a VERY long time for the high number test cases.
-
\$\begingroup\$ Same bytecount:
f=lambda n,k=0:k*(n<2)or f(n-max(d*(n%d<1)for d in range(1,n)),k+1)\$\endgroup\$movatica– movatica2022年07月19日 22:19:36 +00:00Commented Jul 19, 2022 at 22:19
Matlab, 58 bytes
function p=l(a),p=0;if(a-1),p=1+l(a-a/min(factor(a)));end
Vyxal s, 6 bytes
(∆ṫ↔v†
Port of Jelly.
(∆ṫ↔v†
( ↔ # Repeatedly apply until the result does not change, collecting intermediate results:
∆ṫ # Euler's totient
v† # For each, get the number of distinct prime factors
# s flag sums
Vyxal l, (削除) 6 (削除ここまで) 5 bytes
≬K ̄÷ẋ
This is the same as the 6 byte answer below except the length is taken by the l flag rather than a L element.
Vyxal, (削除) 7 (削除ここまで) 6 bytes
≬K ̄÷ẋL
≬ # 3-element lambda:
K # Push a list of the divisors, from 1 to the number itself in increasing order
̄ # Deltas - returns a list of the consecutive differences in the list.
# The resulting list has a length of 1 less than the one fed to it.
÷ # Unwrap the list onto the stack. For a non-empty list, this is effectively
# equivalent to t (Tail - get the last item). But for an empty list, the
# result is effectively whatever was already on the stack, i.e. the the
# number whose list of divisors was taken, i.e., 1, the only one that yields
# an empty deltas list. This makes 1, instead of 0, the fixed point.
ẋ # Repeat the lambda on the number at the top of the stack (which is initially
# the input) until the result no longer changes, returning a list of the
# results. The last result will be 1, our fixed point.
L # Length
This is very slow for most numbers of more than 60 decimal digits or so, since it has to generate a full list of divisors (even if it only uses the largest two). Prime factorization is still fast enough at that level, but unless the number has only one distinct prime factor, the list of divisors will be orders of magnitude longer.
-
\$\begingroup\$ @Steffan Nice work. And now I've beaten yours by 1 byte again :-) \$\endgroup\$Deadcode– Deadcode2022年07月13日 05:41:17 +00:00Commented Jul 13, 2022 at 5:41
Japt, 12 bytes
@!(UμUk ×ばつ}a
How it works
@ !(Uμ Uk Å ×ばつ }a
XYZ{!(U-=Uk s1 r*1 }a
// Implicit: U = input integer
XYZ{ }a // Return the smallest non-negative integer X which returns
// a truthy value when run through this function:
Uk // Take the prime factorization of U.
s1 // Slice off the first item.
// Now we have all but the smallest prime factor of U.
r*1 // Reduce the result by multiplication, starting at 1.
// This takes the product of the array, which is the
// largest divisor of U.
U-= // Subtract the result from U.
!( // Return !U (which is basically U == 0).
// Since we started at 0, U == 1 after 1 less iteration than
// the desired result. U == 0 works because the smallest
// divisor of 1 is 1, so the next term after 1 is 0.
// Implicit: output result of last expression
This technique was inspired by the 05AB1E answer. A previous version used 2¤ (push a 2, slice off the first two items) in place of Å because it's one byte shorter than s1 (note trailing space); I only realized after the fact that because this appends a 2 to the end of the array and slices from the beginning, it in fact fails on any odd composite number, though it works on all given test cases.
><>, 32 bytes
<\?=2:-$@:$/:
1-$:@@:@%?!\
;/ln
Expects the input number, n, on the stack.
This program builds the complete sequence on the stack. As the only number that can lead to 1 is 2, building the sequence stops when 2 is reached. This also causes the size of the stack to equal the number of steps, rather than the number of steps +1.
Ruby, 43 bytes
f=->x{x<2?0:1+f[(1..x).find{|i|x%(x-i)<1}]}
Find the smallest number i such that x divides x-i and recurse until we reach 1.
Haskell, 67 bytes
Here's the code:
a&b|b<2=0|a==b=1+2&(b-1)|mod b a<1=1+2&(b-div b a)|1<2=(a+1)&b
(2&)
And here's one reason why Haskell is awesome:
f = (2&)
(-->) :: Eq a => a -> a -> Bool
(-->) = (==)
h=[f(5) --> 3
,f(30) --> 6
,f(31) --> 7
,f(32) --> 5
,f(100) --> 8
,f(200) --> 9
,f(2016^155) --> 2015
]
Yes, in Haskell you can define --> to be equivalent to ==.
Matlab, 107 bytes
a=input('');b=factor(a-isprime(a));c=log2(a);while(max(b)>1),b=max(factor(max(b)-1));c=c+1;end,disp(fix(c))
- Non-competing, this is not the iterative translation of my last submission, just another direct algerbraic method, it sums up all binary-logs of all prime factors, kinda ambiguous to illustrate.
- I will golf this more when i have time.
MATL, (削除) 17 (削除ここまで) 16 bytes
`tttYfl)/-tq]vnq
Explanation
% Implicitly grab input
` % Do while loop
ttt % Make three copies of top stack element
Yf % Compute all prime factors
l) % Grab the smallest one
/ % Divide by this to get the biggest divisor
- % Subtract the biggest divisor
t % Duplicate the result
q % Subtract one (causes loop to terminate when the value is 1). This
% is functionally equivalent to doing 1> (since the input will always be positive)
% with fewer bytes
] % End do...while loop
v % Vertically concatenate stack contents (consumes entire stack)
n % Determine length of the result
q % Subtract 1 from the length
% Implicitly display result
C99, (削除) 62 (削除ここまで) 61 bytes
1 byte golfed off by @Alchymist.
f(a,c,b)long*c,a,b;{for(*c=0,b=a;a^1;a%--b||(++*c,b=a-=b));}
Call as f(x,&y), where x is the input and y is the output.
-
\$\begingroup\$ If you test a%--b then you can avoid the b-- at the end. A whole one byte saving. \$\endgroup\$Alchymist– Alchymist2016年05月11日 15:06:48 +00:00Commented May 11, 2016 at 15:06
Clojure, (削除) 116 (削除ここまで) 104 bytes
(fn[n](loop[m n t 1](let[s(- m(last(filter #(=(rem m %)0)(range 1 m))))](if(< s 2)t(recur s (inc t))))))
-12 bytes by filtering a range to find multiples, then using last one to get the greatest one
Naïve solution that basically just solves the problem as it's described by the OP. Unfortunately, finding the greatest divisor alone takes up like half the bytes used. At least I should have lots of room to golf from here.
Pregolfed and test:
(defn great-divider [n]
; Filter a range to find multiples, then take the last one to get the largest
(last
(filter #(= (rem n %) 0)
(range 1 n))))
(defn sub-great-divide [n]
(loop [m n
step 1]
(let [g-d (great-divider m) ; Find greatest divisor of m
diff (- m g-d)] ; Find the difference
(println m " is " g-d " --> " m " - " g-d " = " diff)
(if (< diff 2)
step
(recur diff (inc step))))))
(sub-great-divide 30)
30 is 15 --> 30 - 15 = 15
15 is 5 --> 15 - 5 = 10
10 is 5 --> 10 - 5 = 5
5 is 1 --> 5 - 1 = 4
4 is 2 --> 4 - 2 = 2
2 is 1 --> 2 - 1 = 1
6
-
1\$\begingroup\$ @insertusernamehere No, unfortunately, because those are all valid identifiers. I've removed all the possible whitespace. If I want to golf it further, I'll need to rework the algorithm. \$\endgroup\$Carcigenicate– Carcigenicate2017年01月18日 17:38:41 +00:00Commented Jan 18, 2017 at 17:38
Perl 6, 35 bytes
{+({$_ -first $_%%*,[R,] ^$_}...1)}
How it works
{ } # A bare block lambda.
[R,] ^$_ # Construct range from arg minus 1, down to 0.
first $_%%*, # Get first element that is a divisor of the arg.
$_ - # Subtract it from the arg.
{ }...1 # Do this iteratively, until 1 is reached.
+( ) # Return the number of values generated this way.
Stax, (削除) 9 (削除ここまで) 7 bytes
Åiw^Å♫┌
These two operations are identical.
- Take the biggest divisor of
n– which is different fromnitself – and subtract it fromn. - Let
dbe the smallest prime divisor ofn. Calculaten * (d - 1) / d.
I like #2 better, so let's count how many times we can do that one instead. So at each step, we decrement the smallest prime factor. Let's work an example starting with 30.
Step n Factors Operation
0 30 [2, 3, 5] Replace 2 with 1
1 15 [1, 3, 5] = [3, 5] Replace 3 with 2
2 10 [2, 5] Replace 2 with 1
3 5 [1, 5] = [5] Replace 5 with 4
4 4 [4] = [2, 2] Replace 2 with 1
5 2 [1, 2] = [2] Replace 2 with 1
6 1 [1] = [] End of the line
From the example we can see a recursive definition for steps(x) that solves the problem. Presented here in python-ish pseudo-code.
def steps(n):
pf = primeFactors(n)
return sum(steps(d - 1) for d in pf) + 1
The stax program, unpacked into ascii looks like this.
Z push a zero under the input
|fF for each prime factor of the input, run the rest of the program
v decrement the prime factor
G run the *whole* program from the beginning
+^ add to the running total, then increment
-
\$\begingroup\$ Looks like we have a new winner. A short question: Is there a meta agreement that characters count as bytes on Stax? I would say these are 7 characters and 13 bytes. \$\endgroup\$insertusernamehere– insertusernamehere2018年08月17日 17:17:56 +00:00Commented Aug 17, 2018 at 17:17
-
\$\begingroup\$ You can read about the character encoding stax uses here. If you doubt the byte count, you can download the program using the down arrow button, and count the bytes yourself. It will show you that the program is 7 bytes. To verify, you can re-upload the program and run it to verify the behavior is the same. \$\endgroup\$recursive– recursive2018年08月17日 19:19:34 +00:00Commented Aug 17, 2018 at 19:19
Jelly, 8 bytes
ÆḌṀạƊƬL’
A different approach that takes advantage of Jelly's newer features.
How it works
ÆḌṀạƊƬL’ - Main link. Takes n on the left
Ɗ - Group the previous 3 links as a monad f(n):
ÆḌ - Proper divisors of n
Ṁ - The largest
ạ - Absolute different with n
Ƭ - Repeatedly run f(n) on n, updating n with the result, until
reaching a fixed point (1), collecting intermediate steps
L - Take the length
’ - Decrement to account for the 1
2^32 - 1. The rest is up to you and your system. Hope, this is what you meant with your question. \$\endgroup\$