The Wayback Machine - https://web.archive.org/web/20210114105947/https://www.geeksforgeeks.org/smallest-multiple-of-n-with-exactly-n-digits-in-its-binary-number-representation/
Related Articles

Related Articles

Smallest multiple of N with exactly N digits in its Binary number representation
  • Last Updated : 17 Jun, 2020

Given a positive integer N, the task is to find the smallest multiple of N with exactly N digits in its binary number representation.

Example:

Input: N = 3
Output: 6
Explanation:
6 is the smallest multiple of 3 and has length also 3(110) in binary.

Input: N = 5
Output: 20
Explanation:
6 is the smallest multiple of 5 and has length also 5(10100) in binary.

Approach: The idea is to make an observation.



  • If we observe carefully a series will be formed as 1, 2, 6, 8, 20, …
  • The N-th term in the series would be:

     N*\lceil 2^\frac{N-1}{N} \rceil

  • Therefore, the number N is taken as the input and the above formula is implemented.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find smallest
// multiple of n with exactly N
// digits in Binary number System.
  
#include <iostream>
#include <math.h>
using namespace std;
  
// Function to find smallest multiple
// of n with exactly n digits
// in Binary number representation.
void smallestNumber(int N)
{
    cout << N * ceil(pow(2,
                         (N - 1))
                     / N);
}
  
// Driver code
int main()
{
    int N = 3;
    smallestNumber(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find smallest 
// multiple of n with exactly N 
// digits in Binary Number System. 
class GFG{ 
  
// Function to find smallest 
// multiple of n with exactly N 
// digits in Binary Number System. 
static void smallestNumber(int N) 
{ 
    System.out.print(N * Math.ceil
                        (Math.pow(2, (N - 1)) / N)); 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    int N = 3; 
      
    smallestNumber(N);
} 
} 
  
// This code is contributed by shubham

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find smallest
# multiple of n with exactly N
# digits in Binary number System.
from math import ceil
  
# Function to find smallest multiple
# of n with exactly n digits
# in Binary number representation.
def smallestNumber(N):
    print(N * ceil(pow(2, (N - 1)) / N))
  
# Driver code
N = 3
smallestNumber(N)
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find smallest 
// multiple of n with exactly N 
// digits in Binary Number System.
using System;
  
class GFG{ 
  
// Function to find smallest 
// multiple of n with exactly N 
// digits in Binary Number System. 
static void smallestNumber(int N) 
{ 
    Console.Write(N * Math.Ceiling(
                      Math.Pow(2, (N - 1)) / N)); 
} 
      
// Driver code 
public static void Main(string[] args) 
{ 
    int N = 3; 
          
    smallestNumber(N);
} 
} 
  
// This code is contributed by AnkitRai01

chevron_right


Output:

6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :