Definisi
a(0) = 0
a(n) = n-a(a(a(n-1)))
untuk integern > 0
Tugas
Diberikan bilangan bulat non-negatif n
, keluaran a(n)
.
Testcases
n a(n)
0 0
1 1
2 1
3 2
4 3
5 4
6 4
7 5
8 5
9 6
10 7
11 7
12 8
13 9
14 10
15 10
16 11
17 12
18 13
19 13
20 14
10000 6823
Referensi
code-golf
sequence
number-theory
recursion
Biarawati Bocor
sumber
sumber
Jawaban:
Haskell,
2322 byteCukup gunakan definisi urutan.
f(f$f$n-1)
setara denganf (f (f (n-1)))
.Uji:
Terima kasih kepada Anders Kaseorg untuk satu byte!
sumber
(f$f$f$n-1)
=f(f$f$n-1)
menyimpan satu byte.Jelly , 8 byte
Cobalah online! atau verifikasi kasus uji yang lebih kecil .
Bagaimana itu bekerja
sumber
Mathematica, 20 byte
Hitungan byte mengasumsikan penyandian ISO 8859-1 (atau yang kompatibel) dan
$CharacterEncoding
diatur ke nilai yang cocok, seperti standar WindowsWindowsANSI
.Ini mendefinisikan operator unary
±
.sumber
PlusMinus
. Lihat posting ini untuk detailnya.@
atau[ ]
terlalu.J,
1412 byteDisimpan 2 byte berkat @ Leaky Nun .
Menghitung hasil dengan menyebut dirinya secara rekursif ketika n > 0 tiga kali pada n -1 dan mengurangi hasil itu dari n . Ada situasi yang berbeda untuk kasus dasar ketika n = 0. Di sana itu menghitung n - n yang sama dengan 0.
Coba di sini.
Penjelasan
sumber
Julia, 16 byte
Cobalah online!
Bagaimana itu bekerja
Kami mendefinisikan kembali operator unary
!
untuk tujuan kami.Jika n = 0 , perbandingan
n>0
menghasilkan false dan begitu juga!
.Jika tidak, kode setelah
&&
dieksekusi.~-n
sama dengan(n-1)
komplemen dua,!!!
secara rekursif memanggil!
tiga kali pada n - 1 , dan nilai yang dihasilkan dikurangi dari n .sumber
-!!~-
._.!
hanyalah nama fungsi.Python, 31 byte
Recursion limit and time constraints make above function impractical, but in theory it should work (and does work for small n).
sumber
JavaScript (ES6), 52 bytes
I could have been boring and written the recursive version but this version is much faster (easily coping with the last test case) and also uses
reduce
so that's a plus!sumber
Retina,
4943 bytesTry it online!
sumber
CJam,
1312 bytesThanks to Dennis for saving 1 byte.
Test it here.
sumber
a
.R,
4241 bytesUsage:
This recursive approach doesn't scale well for larger values of
n
though.sumber
n<1
. As its a sequence, it is only really defined for non-negative integers anyhow.a=function(n)"if"(n,n-a(a(a(n-1))),0)
will work for several bytes off.Oasis, 6 bytes
Code:
Expanded version:
Code:
Try it online!
sumber
Sesos,
5855 bytesHandles inputs up to 400 reasonably well, but run time increases dramatically after that point.
Try it online! Check Debug to see the generated SBIN code.
Sesos assembly
The binary file above has been generated by assembling the following SASM code.
sumber
LISP, 61 bytes
Probably not the optimal solution, but it works.
sumber
Java 7, 42 bytes
Ungolfed & test cases:
Try it here.
Output:
sumber
Ruby, 27 bytes
The obvious implementation.
This is a longer, faster answer that caches previous entries in the sequence. Both answers only work for versions after 1.9, as that was when
->
, the stabby lambda, was introduced to Ruby.sumber
C#, 35 bytes
sumber
Golfscript,
2625 bytesTry it online!
Locally
10000
takes less than half a second.sumber
C,
3532 bytesSaved 3 bytes thanks to @PeterTaylor!
Try it on Ideone!
sumber
a(n){return n?n-a(a(a(n-1))):0;}
:
in your code too. You should take out the one after?
.Javascript ES6, 22 bytes
I'll be boring and do the recursive version :P
sumber
VBA, 69 bytes
Works in the blink of an eye on the test set, slows down a little above n=1000000, runs into a memory wall a little above n=25 million.
sumber
Pyth, 10 bytes
Defines a function
y
. Try it online: DemonstrationThis uses a relative new feature of Pyth. You can apply a function multiple times using the fold-syntax. It doesn't actually save any bytes, I used it just for demonstration purposes.
Explanation:
sumber
Maple,
2826 bytesUsage:
sumber
dc, 34 bytes
Input is taken from the top of the stack. This must be the only item on the stack, because the stack depth is used as a counter. Example of usage:
This is a fairly straightforward implementation of the sequence definition:
Anyways, it started out straightforward...then the golfing happened.
sumber
Common Lisp, 44 bytes
Try it online!
sumber
C++ ( MSVC, mainly )
Normal version : 40 bytes
Template meta programming version : 130 bytes
Usage :
The template version is the fastest code, since there's nothing faster than moving a value into a register => with optimization,
H<20>::a()
compile as :For 10000, the recursive version crashes due to a stack overflow error, and the template version crashes at compile time due to template instantiation depth. GCC goes to 900 ( 614 )
sumber
C
and{
in the template meta programming versionD, 36 bytes
Try it online!
sumber
APL (Dyalog Unicode),
1817 bytesTry it online!
Surprisingly, there's no APL answer to this challenge. This is a literal implementation of the function in the OP.
TIO times out forn>90 .
Saved a byte thanks to @Zacharý.
sumber
{⍵=0:0⋄⍵-∇⍣3⊢⍵-1}
Python 3, 72 bytes
Ideone it!
sumber
PowerShell v2+, 56 bytes
The PowerShell equivalent of a lambda to form the recursive definition. Execute it via the
&
call operator, e.g.&$a(5)
. Takes a long time to execute -- even50
on my machine (a recent i5 with 8GB RAM) takes around 90 seconds.Faster iterative solution, 59 bytes
Longer only because we need to account for input
0
(that's the*!!$n
at the end). Otherwise we just iteratively construct the array up to$n
, adding a new element each time, and output the last one at the end$o[-1]
. Super-speedy -- calculating10000
on my machine takes about 5 seconds.sumber
><>, 55+2 = 57 bytes
The input is expected to be present on the stack at program start, so +2 bytes for the
-v
flag. Try it online!This is hecka slow, as it uses recursion to calculate the result. Using TIO,
h(50)
takes over a minute. It returns the correct results <= 30, so I'm confident it would work forh(10000)
, I just haven't run it to find out!sumber