Given a number N denoting candies. Find how many packets are needed to contain the number if 1st packet can contain 1 candy, 2nd packet can contain 2 candies, 3rd packet can contain 4 candies and so on.
Example 1:
Input:
N = 2
Output:
2
Explanation:
Put 1 candy in first packet.
Put 1 candy in second packet.
Thus two,packets are required.
Example 2:
Input:
N = 8
Output:
4
Explanation:
Put 1 candy in first packet.
Put 2 candies in second packet.
Put 4 candies in third packet.
Put 1 candy in fourth packet.
Thus 4 packets are required.
Your Task:
You don't need to read input or print anything.Your Task is to complete the function countPackets() which takes a number N as input parameter and returns the number of packets required to contain it.
Expected Time Complexity:O(logN)
Expected Auxillary Space:O(1)
Constraints:
1<= N <= 10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def countPackets(self,N):

        #code here

Solutions:
class Solution:

	def countPackets(self, N):
		c = 0
		while N:
			N //= 2
			c += 1
		return c


class Solution:

	def countPackets(self, N):
		packets = 1
		candies = 1
		k = 1
		while N - candies > 0:
			candies += 2 * k
			k *= 2
			packets += 1
		return packets


from math import *

class Solution:

	def countPackets(self, N):
		return ceil(log(N + 1, 2))