| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | Vyxal | 241102T032814Z | emanresu |
| 029 | Uiua | 241102T015554Z | nyxbird |
| 018 | iogii | 241101T154605Z | Darren S |
| 073 | AWK | 241101T164806Z | xrs |
| 017 | Japt | 170905T225102Z | Shaggy |
| 050 | JavaScript ES6 | 170905T223219Z | Shaggy |
| 059 | Haskell | 170905T233411Z | nimi |
| 053 | Python 2 | 170905T221500Z | totallyh |
| 058 | Mathematica | 170905T223111Z | ZaMoC |
| 018 | Pyth | 170906T084846Z | Jakube |
| 112 | Clojure v1.8 | 170906T070721Z | Chris |
| 098 | Java 8 | 170906T075137Z | Kevin Cr |
| 016 | 05AB1E | 170906T062533Z | Emigna |
| 013 | Husk | 170906T055929Z | Zgarb |
| 022 | Pyth | 170906T052125Z | Mr. Xcod |
| 034 | Golfscript | 170906T023914Z | Josiah W |
| nan | VB.NET .NET 4.5.2 | 170906T023035Z | Brian J |
| nan | Perl 5 | 170905T224137Z | Xcali |
| 015 | Jelly | 170905T232417Z | Jonathan |
| 017 | Jelly | 170905T231623Z | miles |
| 4140 | Perl 6 | 170905T222423Z | Ramillie |
Vyxal, 10 bytes
½›n²∑"i)İL
İL # Count the unique values from...
-------) # Applying the following function
i # Index, modularly, into
" # The pair of
½› # n/2+1 if n is even
n²∑ # And the sum of the digits of n^2 if n is odd
Uiua, 29 bytes
⧻[⍢(⨬(+1÷2|/+⍜°⋕≡¤ⁿ2)⊸◿2.)≠₉]
1-indexed (if 2-indexing was allowed, we could replace ⍢ with ⍥ and ≠₉ with ∞ for -1 byte)
⧻[⍢(⨬(+1÷2|/+⍜°⋕≡¤ⁿ2)⊸◿2.)≠₉]
⍢( )≠₉ # while n≠9:
[ . ] # push n into an array
⨬( | )⊸◿2 # switch on n%2:
( | # 0:
+1÷2 # n ← n/2+1
| ) # 1:
ⁿ2 # square
⍜°⋕≡¤ # get digits
/+ # and sum
⧻ # get the length of the array
iogii 18 bytes
i;2%$*\_$2/)if?wz{
Try it! (0 indexed)
If you want to see all test cases we must change \ to \, otherwise it would mean transpose instead of digits: All test cases
I don't really know how to explain it beyond saying what each character does and how the parse would look in something like Haskell:
i iterate
; mdup (put orig value after putting function applied to value)
2
%
$ arg
*
\ digits
_ sum
$ arg
2
/
) inc
if
? isDebut?
w takeWhile
z last
{ whole numbers
last (takeWhile wholeNumbers (isDebut (iterate implicitInput (\i ->
if (i%2) then (i/2+1) else sum (digits (i*i)) end
))))
The mdup is used instead of arg again so that the end block of iterate can be implicit (first time stack size goes back to 1 is after the if).
Using is debut instead of =9 saves a byte
AWK, 73 bytes
{for(j=$1;j!=9;++k){l=split(j%2?j^2:j/2+1,g,X);for(j=0;l;)j+=g[l--]}}$0=k
Zero index as it saves a few bytes.
{for(j=$1;j!=9;++k){ # main loop until 9
l=split( # number into digits
j%2?j^2:j/2+1,g,X); # maths, split into array g
for(j=0;l;)j+=g[l--] # add digits together
}}$0=k # set output to iterations
Japt, 22 21 17 bytes
-9©ÒßUg[U/2ÄU²ìx]
-9©ÒßUg[U/2ÄU²ìx] :Implicit input of integer U
-9 :Subtract 9
© :Logical AND with
Ò : Negate the bitwise NOT of
ß : Recursive call with argument
Ug : Index U into (0-based & modular)
[ : Array containing
U/2Ä : 1. U/2+1
U² : 2. U squared
ì : To digit array
x : Reduce by addition
] : End array
JavaScript (ES6), 59 50 bytes
0-indexed.
f=n=>n-9&&f(n%2?eval([...""+n*n].join`+`):n/2+1)+1
Try it
o.innerText=(
f=n=>n-9&&f(n%2?eval([...""+n*n].join`+`):n/2+1)+1
)(i.value=5);oninput=_=>o.innerText=f(+i.value)
<input id=i min=3 type=number><pre id=o>
Explanation
The first thing we do is calculate n-9. If n==9 then that, obviously, gives 0 and things stop there. If n!=9 then n-9 will give a non-zero value which, being truthy, means we can continue on through the logical AND. We call the function again, passing a new n to it, calculated as follows:
n%2?
If n modulo 2 is truthy - i.e., n is odd.
[...""+n*n]
Multiply n by itself, convert it to a string and destructure that string to an array of individual characters (digits).
.join`+`
Rejoin the characters to a string using +, giving us a mathematical expression.
eval( )
Evaluate that expression, giving us the sum of the digits of n*n.
:n/2+1
If n%2 is falsey (i.e., n is even) then we simply divide n by 2 and add 1.
To the result of calling the function again, we then add 1. So, using an initial input of 5, the process goes as follows:
f(5)
= -4&&f(7)+1
= -2&&(f(13)+1)+1
= 4&&((f(16)+1)+1)+1
= 7&&(((f(9)+1)+1)+1)+1
= (((0+1)+1)+1)+1
= 4
Haskell, 62 59 bytes
f 9=0
f a=1+f(cycle[div a 2+1,sum[read[d]|d<-show$a^2]]!!a)
Edit: -3 bytes thanks to @Ørjan Johansen.
Python 2, 129 126 76 68 67 64 54 53 bytes
-3 bytes thanks to Jonathan Frech. -8 bytes thanks to Maltysen. -7 bytes thanks to Jonathan Allan. -1 byte thanks to Mr. Xcoder.
f=lambda n:n-9and-~f(n%2*sum(map(int,`n*n`))or 1+n/2)
From somebody who probably doesn't know enough math, this seems completely arbitrary. :P
Mathematica, 58 bytes
1-indexed
If[#!=9,#0@If[OddQ@#,Total@IntegerDigits[#^2],#/2+1]+1,0]&
Try it online! (in order to work on Mathics, Tr is replaced with Total)
here is -1 byte version by @JungHwanMin (but it doesn't work on mathics so I kept both)
Mathematica, 57 bytes
If[#!=9,#0@If[2∣#,#/2+1,Total@IntegerDigits[#^2]]+1,0]&
Pyth, 18 bytes
tl.u?%N2sj*NNTh/N2
Try it online: Demonstration
Explanation:
tl.u?%N2sj*NNTh/N2
.u apply the following function to the input,
until it runs into a fixed point
?%N2 if value % 2 == 1:
*NN value * value
j T convert to digits
s sum
else:
/N2 value / 2
h + 1
l get the length of all visited values
t - 1
Clojure v1.8, 124 113 112 bytes
0-indexed
(fn[n](loop[a n k 0](if(= a 9)k(recur(if(even? a)(+(/ a 2)1)(apply +(map #(-(int %)48)(str(* a a)))))(inc k)))))
Explanation
(loop[a n k 0](if(= a 9)...)) Loop until a=9
(if(even? a)(+(/ a 2)1)...) If even, a(n, k) / 2 + 1 if a(n, k)
(if(even? a)...(apply +(map #(-(int %)48)(str(* a a))))) If odd, calculate the sum of digits of a(n, k)²
#(-(int %)48) Convert character to number
Java 8, 110 98 bytes
n->{int k=0,s;for(;n!=9;k++){s=0;for(int c:(n*n+"").getBytes())s+=c-48;n=n%2<1?n/2+1:s;}return k;}
0-indexed
Explanation:
n-> // Method with integer as both input and return-type
int k=0, // Result-integer `k` starting at 0
s; // Sum-integer
for(;n!=9; // Loop (1) as long as `n` is not 9
k++){ // And increase `k` by 1 after every iteration
s=0; // Reset sum `s` to 0
for(int c:(n*n+"").getBytes())
// Do `n*n` and inner loop (2) over the digits as characters
s+=c-48; // And increase the sum `s` with these digits
// End of inner loop (2) (implicit / single-line body)
n=n%2<1? // If `n` is even:
n/2+1 // Change `n` to `n/2+1`
: // Else:
s; // Change `n` to sum `s`
} // End of loop (1)
return k; // Return the result `k`
} // End of separated method (2)
05AB1E, 16 bytes
[Ð9Q#Èi2÷>ënSO]N
Explanation
[ # start a loop
Ð # triplicate current number
9Q# # if it equals 9, break
Èi # if even
2÷> # divide by 2 and increment
ë # else
n # square
SO # sum digits
] # end loop
N # push the iteration counter N
Husk, 13 bytes
€9¡?o→½ȯΣd□¦2
This is 1-indexed. Try it online!
Explanation
Nothing too fancy here.
€9¡?o→½ȯΣd□¦2 Implicit input, say n = 5
¡ Iterate the following function:
? ¦2 If divisible by 2,
o→½ then halve and increment,
ȯΣd□ else square, take digits and get their sum.
This gives an infinite sequence: [5,7,13,16,9,9,9,9,9..
€9 1-based index of 9; print implicitly.
Pyth, 23 22 bytes
For now, this is a recursive function but I'll try to switch to ..W (functional while) to save bytes instead
L&-b9hy|*%b2sj^b2Th/b2
Try it here! (with additional code to call the function - use y<your_number> - without spaces)
Golfscript, 34 bytes
I really need a better way than I have to add up the digits of a number.
~{9-}{.2%{.*`{+48-}*48-}{2/)}if}/,
VB.NET (.NET 4.5.2), 107 + 20 (imports) = 117 bytes
Requires Imports System.Linq
Function A(n)
While n<>9
n=If(n Mod 2=0,n/2+1,CStr(n^2).Sum(Function(c)Val(c)))
A+=1
End While
End Function
Function which takes n as an integer input and returns a 0-based k.
Ungolfed:
Function A(n) ' input/output types are Object, but we will be casting to integer
'A = 0 ' VB will create an implicit variable with the same name as the function
' loop until a(n, k) = 9
' using n as the variable to store a(n, k)
While n <> 9
n = If(n Mod 2 = 0, ' equivalent to c# ternary ?: operator
n / 2 + 1, ' even case
CStr(n ^ 2).Sum(Function(c) Val(c)))
' odd case
' cast number to string
' then convert each char to the number it represents
' and do a linq sum
A += 1 ' Object + Integer will coerce to an integer
End While
' Where's the return?
' That implicit variable with the matching name will get returned if there's no explicit return
End Function
Perl 5, 56 + 1 (-n) = 57 bytes
$|++,$_=$_%2?eval$_**2=~s/./+$&/gr:1+$_/2while$_-9;say$|
Jelly, 16 15 bytes
-1 byte thanks to miles (use of ternary if)
²DSµH‘µḂ?_9$пL
A monadic link taking and returning numbers.
1-indexed
Try it online! or see a test-suite (coerces results to be 0-indexed and formats like OP code-block)
How?
²DSµH‘µḂ?_9$пL - Link: number, n
п - collect results in a list while:
$ - last two links as a monad:
_9 - subtract nine
? - if:
Ḃ - bit - current loop input modulo by 2 (1 if odd, 0 if even)
µ - ...then:
² - square the current loop input
D - cast to a list of its decimal digits
S - sum
µ - ...else:
H - halve current loop input
‘ - increment
L - length (get the number of results collected
- - this includes the 9, so is 1-indexed w.r.t. k)
Jelly, 17 bytes
²DSµH‘$Ḃ?ßµ-n9$?‘
Straight-forward approach. Uses 0-based indexing.
Explanation
²DSµH‘$Ḃ?ßµ-n9$?‘ Input: n
? If
n9$ n != 9
µ Then
? If
Ḃ n % 2 == 1
µ Then
² Square
D Decimal digits
S Sum
$ Else
H Halve
‘ Increment
ß Call recursively
Else
- The constant -1
‘ Increment
Perl 6, 41 bytes (40 chars)
{+($_,{$_%2??[+] $_².comb!!$_/2+1}...9)}
This uses 1-indexing of k's, so it gives 1 higher answers than the examples in OP. If this is not what the 1-indexing means, I'll have to add 1 byte more.
Explanation: It's an anonymous function. We just use the Perl 6's facility for generating lists using recursion :—). It looks like this: (first element),(block that takes the previous element and gives the next)...(end condition). In this case, the first element is $_ (argument of the main function) and the end condition is 9 (fulfilled when we generate a 9). In the middle block, we use $_ to refer to its argument ( = the previous element of the sequence). The ?? !! is the old ternary operator (better known as ? :). Finally, we take the length of this list by forcing numerical context by +(...).
The last weird thing here is the sum of digits. Numbers are Cool (behave both like strings and numbers), so we use a string method .comb on $_² (give list of characters = digits), then adding the characters up (that converts them back to numbers).