Given an integer n, the task is to count the total lucky numbers smaller than or equal to n. A number is said to be lucky if it has all contagious number of 1’s in binary representation from the beginning. For example 1, 3, 7, 15 are lucky numbers, and 2, 5 and 9 are not lucky numbers.
Examples:
Input :n = 7 Output :3 1, 3 and 7 are lucky numbers Input :n = 17 Output :4
Approach:one approach is that first we find out the binary representation of each number and than check for contagious number of 1’s for each number, but this approach is time consuming and can give tle if the constraints are two large, Efficient approach can be find out by observing the numbers, we can say that every ith lucky number can be found by the formula 2^{i}1, and by iterating a loop upto number less than equal to n we can find out the total lucky numbers.
Below is the implementation of above approach
#include <bits/stdc++.h> using namespace std;
int countLuckyNum( int n)
{ int count = 0, i = 1;
while (1) {
if (n >= ((1 << i)  1))
count++;
else
break ;
i++;
}
return count;
} // Driver code int main()
{ int n = 7;
cout << countLuckyNum(n);
return 0;
} 
# python3 code of above problem # function to count the lucky number def countLuckyNum(n):
count, i = 0 , 1
while True :
if n> = 2 * * i  1 :
count + = 1
else :
break
i + = 1 ;
return count
# driver code n = 7
print (countLuckyNum(n))

import java.util.*;
import java.lang.*;
import java.io.*;
public class GFG {
// Function to return the count of lucky number
static int countLuckyNum( int n)
{
int count = 0 , i = 1 ;
while ( true ) {
if (n >= (( 1 << i)  1 ))
count++;
else
break ;
i++;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int n = 7 ;
System.out.println(countLuckyNum(n));
}
} 
<?php // PHP implementation of the approach // Function to count the lucky number function countLuckyNum( $n )
{ $count = 0;
$i = 1;
while (1)
{
if ( $n >= ((1 << $i )  1))
$count += 1;
else
break ;
$i += 1;
}
return $count ;
} // Driver code $n = 7;
echo countLuckyNum( $n ) ;
?> 
// C# implementation of the approach using System;
public class GFG {
// Function to return the count of lucky number
static int countLuckyNum( int n)
{
int count = 0, i = 1;
while ( true ) {
if (n >= ((1 << i)  1))
count++;
else
break ;
i++;
}
return count;
}
// Driver code
public static void Main()
{
int n = 7;
Console.WriteLine(countLuckyNum(n));
}
} 
output:3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a studentfriendly price and become industry ready.
Recommended Posts:
 Find the occurrence of the given binary pattern in the binary representation of the array elements
 Permutations of n things taken all at a time with m things never come together
 Number of ways to arrange a word such that all vowels occur together
 1 to n bit numbers with no consecutive 1s in binary representation.
 Longest common substring in binary representation of two numbers
 Sum of decimal equivalent of all possible pairs of Binary representation of a Number
 Maximum number of consecutive 1's in binary representation of all the array elements
 Count number of trailing zeros in Binary representation of a number using Bitset
 Count of total bits toggled/flipped in binary representation of 0 to N
 C program to count zeros and ones in binary representation of a number
 Check if all subnumbers have distinct Digit product
 Count numbers from 1 to n that have 4 as a digit
 Count the numbers < N which have equal number of divisors as K
 Count of numbers in range which are divisible by M and have digit D at odd places
 Total number of ways to place X and Y at n places such that no two X are together
 Minimum time required to complete a work by N persons together
 Permutations of n things taken r at a time with k things together
 Number of ways to arrange a word such that no vowels occur together
 Seating arrangement of N boys sitting around a round table such that two particular boys sit together
 Distance Traveled by Two Trains together in the same Direction
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.