Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

As pointed out in Neil's brilliant answerNeil's brilliant answer, the output can never exceed 17, so the digital sum of any output above 9 is equal to n%9. This also works with outputs below 9; we can make it work for 9 as well by subtracting 1 with ~- before the modulus, then adding back in 1 after.


The best I could do with hardcoding is 50 bytes:

n=>"0x"+"7880136ba5867ffedb834968"[n%24]-(n<3)*9+2

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

As pointed out in Neil's brilliant answer, the output can never exceed 17, so the digital sum of any output above 9 is equal to n%9. This also works with outputs below 9; we can make it work for 9 as well by subtracting 1 with ~- before the modulus, then adding back in 1 after.


The best I could do with hardcoding is 50 bytes:

n=>"0x"+"7880136ba5867ffedb834968"[n%24]-(n<3)*9+2

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

As pointed out in Neil's brilliant answer, the output can never exceed 17, so the digital sum of any output above 9 is equal to n%9. This also works with outputs below 9; we can make it work for 9 as well by subtracting 1 with ~- before the modulus, then adding back in 1 after.


The best I could do with hardcoding is 50 bytes:

n=>"0x"+"7880136ba5867ffedb834968"[n%24]-(n<3)*9+2
added 264 characters in body
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

As pointed out in Neil's brilliant answer, the output can never exceed 17, so the digital sum of any output above 9 is equal to n%9. This also works with outputs below 9; we can make it work for 9 as well by subtracting 1 with ~- before the modulus, then adding back in 1 after.


The best I could do with hardcoding is 50 bytes:

n=>"0x"+"7880136ba5867ffedb834968"[n%24]-(n<3)*9+2

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

As pointed out in Neil's brilliant answer, the output can never exceed 17, so the digital sum of any output above 9 is equal to n%9. This also works with outputs below 9; we can make it work for 9 as well by subtracting 1 with ~- before the modulus, then adding back in 1 after.

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

As pointed out in Neil's brilliant answer, the output can never exceed 17, so the digital sum of any output above 9 is equal to n%9. This also works with outputs below 9; we can make it work for 9 as well by subtracting 1 with ~- before the modulus, then adding back in 1 after.


The best I could do with hardcoding is 50 bytes:

n=>"0x"+"7880136ba5867ffedb834968"[n%24]-(n<3)*9+2
added 307 characters in body
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

As pointed out in Neil's brilliant answer, the output can never exceed 17, so the digital sum of any output above 9 is equal to n%9. This also works with outputs below 9; we can make it work for 9 as well by subtracting 1 with ~- before the modulus, then adding back in 1 after.

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

JavaScript (ES6), 34 bytes

f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2

May freeze your browser for inputs above 27 or so, but it does work for all input values. This can be verified with a simple cache:

c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2
<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>

As pointed out in Neil's brilliant answer, the output can never exceed 17, so the digital sum of any output above 9 is equal to n%9. This also works with outputs below 9; we can make it work for 9 as well by subtracting 1 with ~- before the modulus, then adding back in 1 after.

Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240
Loading