Check if a number N starts with 1 in b-base

Given a number N and base b if N in base b representation starts with 1 print Yes else print No
Examples :

Input : n = 6, b = 4
Output : Yes
6 can be written as 12_4 in base 4
so answer is Yes as it starts with 1

Input : n = 24, b = 2
Output : Yes
24 can be written as 11000_2 in base 2
so answer is Yes as it starts with 1

Input :  n = 24, b = 7
Output : No
24 can be written as 33_7 in base 7
so answer is No as it starts with 3        

When a number N is represented in base ‘b’ it gets converted to m+1 length sequence b_{m} b_{m-1}…..b_0 which implies b_{m}*b^{m} + b_{m-1}*b^{m-1}…..+b_0*b^0 = N
The smallest number in base b and starting with ‘1’ i.e. 100..00 and m+1 digits in base is b^{m}
and largest number is 2*b^{m}-1.So N should lie in this range.
b^{m} <= N <= 2*b^{m}-1
Now another thing to notice is that m cannot exceed floor(log_2(N)) because when we represent any number in base-2 it gets converted into a sequence of only 1s and 0s so the length of this sequence will always be greater than any other base representation and its length will be equal to floor(log_2(N))+1.
So to check for a given base 'b' if N starts with 1 or not we will traverse from m = 1 to m = floor(log_2(N)) and check if for any m N lies in the range b^{m} <= N <= 2*b^{m}-1 or not and accordingly print "Yes" or "No".

C++

// C++ program to check if number starts with
// one in base b representation
#include <bits/stdc++.h>
using namespace std;
  
// Returns true if n starts with  1 in
// base b represenation
bool CheckIfstartsWithOne(int n, int b)
{
    // highest m can be log2(n)
    int m = log2(n);
    for (int i = 1; i <= m; i++) {
        // if b^m <= N <= 2*b^m - 1,
        // return true
        if (n >= pow(b, i) && n <= 2 * pow(b, i) - 1)
            return true;
    }
    return false;
}
  
// printing yes or no
void printYesORno(int n, int b)
{
    if (CheckIfstartsWithOne(n, b) == true)
        cout << "Yes" << endl;
    else if (CheckIfstartsWithOne(n, b) == false)
        cout << "No" << endl;
}
  
// driver function
int main()
{
    printYesORno(6, 4);
    printYesORno(24, 2);
    printYesORno(24, 7);
    printYesORno(24, 15);
}

Java

// Java program to check if number starts with
// one in base b representation
  
class GFG {
      
    // Returns true if n starts with 1 in
    // base b represenation
    static boolean CheckIfstartsWithOne(int n, int b)
    {
          
        // highest m can be log2(n)
        int m = (int)(Math.log10(n) / Math.log10(2));
        for (int i = 1; i <= m; i++) {
              
            // if b^m <= N <= 2*b^m - 1,
            // return true
            if (n >= (int)Math.pow(b, i) &&
                n <= 2 * (int)Math.pow(b, i) - 1)
                return true;
        }
          
        return false;
    }
  
    // Driver method
    public static void main(String args[])
    {
        System.out.println(
           CheckIfstartsWithOne(6, 4) ? "Yes" : "No");
             
        System.out.println(
           CheckIfstartsWithOne(24, 2) ? "Yes" : "No");
             
        System.out.println(
           CheckIfstartsWithOne(24, 7) ? "Yes" : "No");
             
        System.out.println(
           CheckIfstartsWithOne(24, 15) ? "Yes" : "No");
    }
}

Python3

# Python3 program to check 
# if number starts with one
# in base b representation
import math
  
# Returns true if n 
# starts with 1 in 
# base b represenation
def CheckIfstartsWithOne(n, b):
      
    # highest m can be log2(n)
    m = (int)(math.log2(n));
    for i in range (1, m + 1):
          
        # if b^m <= N <= 2*b^m - 1,
        #return true
        x = (int)(math.pow(b, i));
        if n >= x and n <= 2 * x - 1:
            return 1;
    return 0;
  
# printing yes or no
def printYesORno(n, b):
    if CheckIfstartsWithOne(n, b) == 1:
        print("Yes");
    if CheckIfstartsWithOne(n, b) == 0:
        print("No");
          
# Driver Code
printYesORno(6, 4);
printYesORno(24, 2);
printYesORno(24, 7);
printYesORno(24, 15);
  
# This code is contributed by mits.

C#

// C# program to check if number starts
// with one in base b representation
using System;
  
public class GFG {
      
    // Returns true if n starts with
    // 1 in base b represenation
    static bool CheckIfstartsWithOne(int n, int b)
    {
          
        // highest m can be log2(n)
        int m = (int)(Math.Log10(n) / Math.Log10(2));
          
        for (int i = 1; i <= m; i++)
        {
              
            // if b^m <= N <= 2*b^m - 1,
            // return true
            if (n >= (int)Math.Pow(b, i) && 
                   n <= 2 * (int)Math.Pow(b, i) - 1)
                return true;
        }
          
        return false;
    }
  
    // Driver method
    public static void Main()
    {
        Console.WriteLine(
           CheckIfstartsWithOne(6, 4) ? "Yes" : "No");
             
        Console.WriteLine(
           CheckIfstartsWithOne(24, 2) ? "Yes" : "No");
             
        Console.WriteLine(
           CheckIfstartsWithOne(24, 7) ? "Yes" : "No");
             
        Console.WriteLine(
           CheckIfstartsWithOne(24, 15) ? "Yes" : "No");
    }
}
  
// This code is contributed by Sam007.

PHP

<?php
// PHP program to check if 
// number starts with one 
// in base b representation
  
// Returns true if n starts 
// with 1 in base b represenation
function CheckIfstartsWithOne($n, $b)
{
    // highest m can be log2(n)
    $m = log($n, 2);
    for ($i = 1; $i <= $m; $i++) 
    {
        // if b^m <= N <= 2*b^m - 1,
        // return true
        if ($n >= pow($b, $i) && 
                $n <= 2 * pow($b, $i) - 1)
            return true;
    }
    return false;
}
  
// printing yes or no
function printYesORno($n, $b)
{
    if (CheckIfstartsWithOne($n, $b) == true)
        echo "Yes" ,"\n";
    else if (CheckIfstartsWithOne($n, $b) == false)
        echo "No" ,"\n";
}
  
// Driver Code
printYesORno(6, 4);
printYesORno(24, 2);
printYesORno(24, 7);
printYesORno(24, 15);
  
// This code is contributed by ajit 
?>


Output :

Yes
Yes
No
Yes

This article is contributed by Ayush Jha. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Sam007, jit_t, Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.