A number is a Repunit in base B if it can be represented as a string of three or more 1’s in a base >= 2.
Check if N is a Repunit number
Given an integer N, the task is to check if N is a Repunit number in base B.
Input: N = 31, B = 5
31 can be written as 111 base in 5
Input: N = 5, B = 2
5 is 101 in base 2
Approach: We will count the number of one’s in the base B of a given number N and also count the number of digits in the base B of a given number N. If they are same, print “YES” else print “NO”.
N = 31, B = 5
31 can be written as 111 base in 5, So number of one’s in base B of a given number N = 3 and number of digits in the base B of a given number N = 3
Since both are equal hence 31 is a Repunit number in base 5.
Below is the implementation of the above approach:
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.
- Number of ones in the smallest repunit
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count numbers which can be constructed using two numbers
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count numbers which are divisible by all the numbers from 2 to 10
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Check if a given pair of Numbers are Betrothed numbers or not
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count of numbers upto M divisible by given Prime Numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- How to swap two numbers without using a temporary variable?
- Lucky Numbers
- Ugly Numbers
- Write a program to add two numbers in base 14