Find Nth natural number with exactly two set bits
Here given code implementation process.
/*
Java Program for
Find Nth natural number with exactly two set bits
*/
public class BinaryBits
{
public void nthNoOfTwoBits(int n)
{
int count = n;
int temp = 1;
int num = 1;
int k = 0;
// Display number of element
System.out.println(" Given nth position : " + n);
while (count > 0)
{
num = (num << 1);
if ((count - (k + 1)) <= 0)
{
temp = temp << (count - 1);
System.out.println(" Result : " + (num | temp));
return;
}
else
{
k++;
count -= k;
}
temp = 1;
}
}
public static void main(String[] args)
{
BinaryBits task = new BinaryBits();
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
task.nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
task.nthNoOfTwoBits(22);
}
}
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
// C Program for
// Find Nth natural number with exactly two set bits
#include <stdio.h>
void nthNoOfTwoBits(int n)
{
int count = n;
int temp = 1;
int num = 1;
int k = 0;
// Display number of element
printf(" Given nth position : %d\n", n);
while (count > 0)
{
num = (num << 1);
if ((count - (k + 1)) <= 0)
{
temp = temp << (count - 1);
printf(" Result : %d\n", (num | temp));
return;
}
else
{
k++;
count -= k;
}
temp = 1;
}
}
int main(int argc, char const *argv[])
{
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
nthNoOfTwoBits(22);
return 0;
}
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Find Nth natural number with exactly two set bits
*/
class BinaryBits
{
public: void nthNoOfTwoBits(int n)
{
int count = n;
int temp = 1;
int num = 1;
int k = 0;
// Display number of element
cout << " Given nth position : " << n << endl;
while (count > 0)
{
num = (num << 1);
if ((count - (k + 1)) <= 0)
{
temp = temp << (count - 1);
cout << " Result : " << (num | temp) << endl;
return;
}
else
{
k++;
count -= k;
}
temp = 1;
}
}
};
int main()
{
BinaryBits *task = new BinaryBits();
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
task->nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
task->nthNoOfTwoBits(22);
return 0;
}
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
// Include namespace system
using System;
/*
Csharp Program for
Find Nth natural number with exactly two set bits
*/
public class BinaryBits
{
public void nthNoOfTwoBits(int n)
{
int count = n;
int temp = 1;
int num = 1;
int k = 0;
// Display number of element
Console.WriteLine(" Given nth position : " + n);
while (count > 0)
{
num = (num << 1);
if ((count - (k + 1)) <= 0)
{
temp = temp << (count - 1);
Console.WriteLine(" Result : " + (num | temp));
return;
}
else
{
k++;
count -= k;
}
temp = 1;
}
}
public static void Main(String[] args)
{
BinaryBits task = new BinaryBits();
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
task.nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
task.nthNoOfTwoBits(22);
}
}
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
package main
import "fmt"
/*
Go Program for
Find Nth natural number with exactly two set bits
*/
type BinaryBits struct {}
func getBinaryBits() * BinaryBits {
var me *BinaryBits = &BinaryBits {}
return me
}
func(this BinaryBits) nthNoOfTwoBits(n int) {
var count int = n
var temp int = 1
var num int = 1
var k int = 0
// Display number of element
fmt.Println(" Given nth position : ", n)
for (count > 0) {
num = (num << 1)
if (count - (k + 1)) <= 0 {
temp = temp << (count - 1)
fmt.Println(" Result : ", (num | temp))
return
} else {
k++
count -= k
}
temp = 1
}
}
func main() {
var task * BinaryBits = getBinaryBits()
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
task.nthNoOfTwoBits(10)
// Test B
// num = 22
// output = 129
task.nthNoOfTwoBits(22)
}
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
<?php
/*
Php Program for
Find Nth natural number with exactly two set bits
*/
class BinaryBits
{
public function nthNoOfTwoBits($n)
{
$count = $n;
$temp = 1;
$num = 1;
$k = 0;
// Display number of element
echo(" Given nth position : ".$n.
"\n");
while ($count > 0)
{
$num = ($num << 1);
if (($count - ($k + 1)) <= 0)
{
$temp = $temp << ($count - 1);
echo(" Result : ".($num | $temp).
"\n");
return;
}
else
{
$k++;
$count -= $k;
}
$temp = 1;
}
}
}
function main()
{
$task = new BinaryBits();
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
$task->nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
$task->nthNoOfTwoBits(22);
}
main();
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
/*
Node JS Program for
Find Nth natural number with exactly two set bits
*/
class BinaryBits
{
nthNoOfTwoBits(n)
{
var count = n;
var temp = 1;
var num = 1;
var k = 0;
// Display number of element
console.log(" Given nth position : " + n);
while (count > 0)
{
num = (num << 1);
if ((count - (k + 1)) <= 0)
{
temp = temp << (count - 1);
console.log(" Result : " + (num | temp));
return;
}
else
{
k++;
count -= k;
}
temp = 1;
}
}
}
function main()
{
var task = new BinaryBits();
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
task.nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
task.nthNoOfTwoBits(22);
}
main();
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
# Python 3 Program for
# Find Nth natural number with exactly two set bits
class BinaryBits :
def nthNoOfTwoBits(self, n) :
count = n
temp = 1
num = 1
k = 0
# Display number of element
print(" Given nth position : ", n)
while (count > 0) :
num = (num << 1)
if ((count - (k + 1)) <= 0) :
temp = temp << (count - 1)
print(" Result : ", (num | temp))
return
else :
k += 1
count -= k
temp = 1
def main() :
task = BinaryBits()
# No Binary nth
# 3 11 1
# 5 101 2
# 6 110 3
# 9 1001 4
# 10 1010 5
# 12 1100 6
# 17 10001 7
# 18 10010 8
# 20 10100 9
# 24 11000 10
# 33 100001 11
# 34 100010 12
# 36 100100 13
# 40 101000 14
# 48 110000 15
# 65 1000001 16
# 66 1000010 17
# 68 1000100 18
# 72 1001000 19
# 80 1010000 20
# 96 1100000 21
# 129 10000001 22
# 130 10000010 23
# 132 10000100 24
# 136 10001000 25
# 144 10010000 26
# 160 10100000 27
# 192 11000000 28
# 257 100000001 29
# 258 100000010 30
# etc
# ------------
# ------------
# Test A
# n = 10
# output = 24
task.nthNoOfTwoBits(10)
# Test B
# num = 22
# output = 129
task.nthNoOfTwoBits(22)
if __name__ == "__main__": main()
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
# Ruby Program for
# Find Nth natural number with exactly two set bits
class BinaryBits
def nthNoOfTwoBits(n)
count = n
temp = 1
num = 1
k = 0
# Display number of element
print(" Given nth position : ", n, "\n")
while (count > 0)
num = (num << 1)
if ((count - (k + 1)) <= 0)
temp = temp << (count - 1)
print(" Result : ", (num | temp), "\n")
return
else
k += 1
count -= k
end
temp = 1
end
end
end
def main()
task = BinaryBits.new()
# No Binary nth
# 3 11 1
# 5 101 2
# 6 110 3
# 9 1001 4
# 10 1010 5
# 12 1100 6
# 17 10001 7
# 18 10010 8
# 20 10100 9
# 24 11000 10
# 33 100001 11
# 34 100010 12
# 36 100100 13
# 40 101000 14
# 48 110000 15
# 65 1000001 16
# 66 1000010 17
# 68 1000100 18
# 72 1001000 19
# 80 1010000 20
# 96 1100000 21
# 129 10000001 22
# 130 10000010 23
# 132 10000100 24
# 136 10001000 25
# 144 10010000 26
# 160 10100000 27
# 192 11000000 28
# 257 100000001 29
# 258 100000010 30
# etc
# ------------
# ------------
# Test A
# n = 10
# output = 24
task.nthNoOfTwoBits(10)
# Test B
# num = 22
# output = 129
task.nthNoOfTwoBits(22)
end
main()
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
/*
Scala Program for
Find Nth natural number with exactly two set bits
*/
class BinaryBits()
{
def nthNoOfTwoBits(n: Int): Unit = {
var count: Int = n;
var temp: Int = 1;
var num: Int = 1;
var k: Int = 0;
// Display number of element
println(" Given nth position : " + n);
while (count > 0)
{
num = (num << 1);
if ((count - (k + 1)) <= 0)
{
temp = temp << (count - 1);
println(" Result : " + (num | temp));
return;
}
else
{
k += 1;
count -= k;
}
temp = 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BinaryBits = new BinaryBits();
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
task.nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
task.nthNoOfTwoBits(22);
}
}
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
/*
Swift 4 Program for
Find Nth natural number with exactly two set bits
*/
class BinaryBits
{
func nthNoOfTwoBits(_ n: Int)
{
var count: Int = n;
var temp: Int = 1;
var num: Int = 1;
var k: Int = 0;
// Display number of element
print(" Given nth position : ", n);
while (count > 0)
{
num = (num << 1);
if ((count - (k + 1)) <= 0)
{
temp = temp << (count - 1);
print(" Result : ", (num | temp));
return;
}
else
{
k += 1;
count -= k;
}
temp = 1;
}
}
}
func main()
{
let task: BinaryBits = BinaryBits();
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
task.nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
task.nthNoOfTwoBits(22);
}
main();
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
/*
Kotlin Program for
Find Nth natural number with exactly two set bits
*/
class BinaryBits
{
fun nthNoOfTwoBits(n: Int): Unit
{
var count: Int = n;
var temp: Int = 1;
var num: Int = 1;
var k: Int = 0;
// Display number of element
println(" Given nth position : " + n);
while (count > 0)
{
num = (num shl 1);
if ((count - (k + 1)) <= 0)
{
temp = temp shl(count - 1);
println(" Result : " + (num or temp));
return;
}
else
{
k += 1;
count -= k;
}
temp = 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: BinaryBits = BinaryBits();
// No Binary nth
// 3 11 1
// 5 101 2
// 6 110 3
// 9 1001 4
// 10 1010 5
// 12 1100 6
// 17 10001 7
// 18 10010 8
// 20 10100 9
// 24 11000 10
// 33 100001 11
// 34 100010 12
// 36 100100 13
// 40 101000 14
// 48 110000 15
// 65 1000001 16
// 66 1000010 17
// 68 1000100 18
// 72 1001000 19
// 80 1010000 20
// 96 1100000 21
// 129 10000001 22
// 130 10000010 23
// 132 10000100 24
// 136 10001000 25
// 144 10010000 26
// 160 10100000 27
// 192 11000000 28
// 257 100000001 29
// 258 100000010 30
// etc
// ------------
// ------------
// Test A
// n = 10
// output = 24
task.nthNoOfTwoBits(10);
// Test B
// num = 22
// output = 129
task.nthNoOfTwoBits(22);
}
Output
Given nth position : 10
Result : 24
Given nth position : 22
Result : 129
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment