A patient has n pills to take. Each day he can take either 1 pill or 2 pills until all pills are gone. What will be the total number of different ways of taking n pills?
My approach as follows:
Each day take only one pill - 1 way
Take 2 pill only a single day, other days take only 1 pill - (n-1)C1 ways
Take 2 pill only 2 days, other days take 1 pill - (n-2)C2 ways
...
...
...
Take 2 pill on (n/2) days - (n/2)C(n/2) ways.
Is my algorithm correct? If yes, how can I find its closed form? If not, what might be the correct way?
Help on solving an algorithm
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: Help on solving an algorithm
Check input and AC output for thousands of problems on uDebug!
Re: Help on solving an algorithm
Go to "answer.com".



“The best gifts come from the heart, not the store.”
Re: Help on solving an algorithm
D[1] = 1 (He takes the pill the next day.)
D[2] = 2 (1 + 1 = 2, 2 = 2)
D[3] = 3 (1 + 1 + 1 = 3, 1 + 2 = 3, 2 + 1 = 3) (D[k-1] + D[k - 2])
D[N] = D[N - 1] + D[N - 2]
It's a fibonacci sequence.
D[2] = 2 (1 + 1 = 2, 2 = 2)
D[3] = 3 (1 + 1 + 1 = 3, 1 + 2 = 3, 2 + 1 = 3) (D[k-1] + D[k - 2])
D[N] = D[N - 1] + D[N - 2]
It's a fibonacci sequence.
Re: Help on solving an algorithm
If there is no unique solution, any of the possible solutions!
We offer pass4sure success for wikipedia exam with help of latest security+ certification and Rockefeller University practice questions and the exams of ARM ccie security PMI