Smallest multiple of N with exactly N digits in its Binary number representation
Last Updated :
27 Jun, 2021
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:
- Therefore, the number N is taken as the input and the above formula is implemented.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
void smallestNumber( int N)
{
cout << N * ceil ( pow (2,
(N - 1))
/ N);
}
int main()
{
int N = 3;
smallestNumber(N);
return 0;
}
|
Java
class GFG{
static void smallestNumber( int N)
{
System.out.print(N * Math.ceil
(Math.pow( 2 , (N - 1 )) / N));
}
public static void main(String[] args)
{
int N = 3 ;
smallestNumber(N);
}
}
|
Python3
from math import ceil
def smallestNumber(N):
print (N * ceil( pow ( 2 , (N - 1 )) / N))
N = 3
smallestNumber(N)
|
C#
using System;
class GFG{
static void smallestNumber( int N)
{
Console.Write(N * Math.Ceiling(
Math.Pow(2, (N - 1)) / N));
}
public static void Main( string [] args)
{
int N = 3;
smallestNumber(N);
}
}
|
Javascript
<script>
function smallestNumber(N)
{
document.write(N * parseInt(Math.ceil(Math.pow(2,
(N - 1))
/ N)));
}
let N = 3;
smallestNumber(N);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...