Sum of n numbers with exactly 3 set bits
Here given code implementation process.
/*
Java Program
Sum of n numbers with exactly 3 set bits
*/
public class BinaryBits
{
public void threeActiveBits(int n)
{
int count = n;
int temp = 1;
int num = 2;
int sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < (num >> 1) && count > 0)
{
sum += (num | (num >> 1) | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
System.out.println(" Given n : " + n);
// Display calculate sum
System.out.println(" Result : " + sum);
}
public static void main(String[] args)
{
BinaryBits task = new BinaryBits();
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
task.threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20);
}
}
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
// C Program for
// Sum of n numbers with exactly 3 set bits
#include <stdio.h>
void threeActiveBits(int n)
{
int count = n;
int temp = 1;
int num = 2;
int sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < (num >> 1) && count > 0)
{
sum += (num | (num >> 1) | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
printf(" Given n : %d\n", n);
// Display calculate sum
printf(" Result : %d\n", sum);
}
int main(int argc, char
const *argv[])
{
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
threeActiveBits(20);
return 0;
}
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Sum of n numbers with exactly 3 set bits
*/
class BinaryBits
{
public: void threeActiveBits(int n)
{
int count = n;
int temp = 1;
int num = 2;
int sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < (num >> 1) && count > 0)
{
sum += (num | (num >> 1) | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
cout << " Given n : " << n << endl;
// Display calculate sum
cout << " Result : " << sum << endl;
}
};
int main()
{
BinaryBits *task = new BinaryBits();
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
task->threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
task->threeActiveBits(20);
return 0;
}
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
// Include namespace system
using System;
/*
Csharp Program
Sum of n numbers with exactly 3 set bits
*/
public class BinaryBits
{
public void threeActiveBits(int n)
{
int count = n;
int temp = 1;
int num = 2;
int sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < (num >> 1) && count > 0)
{
sum += (num | (num >> 1) | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
Console.WriteLine(" Given n : " + n);
// Display calculate sum
Console.WriteLine(" Result : " + sum);
}
public static void Main(String[] args)
{
BinaryBits task = new BinaryBits();
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
task.threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20);
}
}
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
package main
import "fmt"
/*
Go Program
Sum of n numbers with exactly 3 set bits
*/
type BinaryBits struct {}
func getBinaryBits() * BinaryBits {
var me *BinaryBits = &BinaryBits {}
return me
}
func(this BinaryBits) threeActiveBits(n int) {
var count int = n
var temp int = 1
var num int = 2
var sum int = 0
for (count > 0) {
num = (num << 1)
for (temp < (num >> 1) && count > 0) {
sum += (num | (num >> 1) | temp)
temp = temp << 1
count--
}
temp = 1
}
// Display number of element
fmt.Println(" Given n : ", n)
// Display calculate sum
fmt.Println(" Result : ", sum)
}
func main() {
var task * BinaryBits = getBinaryBits()
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
task.threeActiveBits(10)
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20)
}
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
<?php
/*
Php Program
Sum of n numbers with exactly 3 set bits
*/
class BinaryBits
{
public function threeActiveBits($n)
{
$count = $n;
$temp = 1;
$num = 2;
$sum = 0;
while ($count > 0)
{
$num = ($num << 1);
while ($temp < ($num >> 1) && $count > 0)
{
$sum += ($num | ($num >> 1) | $temp);
$temp = $temp << 1;
$count--;
}
$temp = 1;
}
// Display number of element
echo(" Given n : ".$n."\n");
// Display calculate sum
echo(" Result : ".$sum."\n");
}
}
function main()
{
$task = new BinaryBits();
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
$task->threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
$task->threeActiveBits(20);
}
main();
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
/*
Node JS Program
Sum of n numbers with exactly 3 set bits
*/
class BinaryBits
{
threeActiveBits(n)
{
var count = n;
var temp = 1;
var num = 2;
var sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < (num >> 1) && count > 0)
{
sum += (num | (num >> 1) | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
console.log(" Given n : " + n);
// Display calculate sum
console.log(" Result : " + sum);
}
}
function main()
{
var task = new BinaryBits();
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
task.threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20);
}
main();
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
# Python 3 Program
# Sum of n numbers with exactly 3 set bits
class BinaryBits :
def threeActiveBits(self, n) :
count = n
temp = 1
num = 2
sum = 0
while (count > 0) :
num = (num << 1)
while (temp < (num >> 1) and count > 0) :
sum += (num | (num >> 1) | temp)
temp = temp << 1
count -= 1
temp = 1
# Display number of element
print(" Given n : ", n)
# Display calculate sum
print(" Result : ", sum)
def main() :
task = BinaryBits()
# Test A
# n = 10
# --------------
# 7 111
# 13 1101
# 14 1110
# 25 11001
# 26 11010
# 28 11100
# 49 110001
# 50 110010
# 52 110100
# 56 111000
# [7 + 13 + 14 + 25 + 26 + 28 + 49 +
# 50 + 52 + 56] = 320
task.threeActiveBits(10)
# Test B
# num = 20
# --------------
# 7 111
# 13 1101
# 14 1110
# 25 11001
# 26 11010
# 28 11100
# 49 110001
# 50 110010
# 52 110100
# 56 111000
# 97 1100001
# 98 1100010
# 100 1100100
# 104 1101000
# 112 1110000
# 193 11000001
# 194 11000010
# 196 11000100
# 200 11001000
# 208 11010000
# [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
# 52 + 56 + 97 + 98 + 100 + 104 + 112 +
# 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20)
if __name__ == "__main__": main()
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
# Ruby Program
# Sum of n numbers with exactly 3 set bits
class BinaryBits
def threeActiveBits(n)
count = n
temp = 1
num = 2
sum = 0
while (count > 0)
num = (num << 1)
while (temp < (num >> 1) && count > 0)
sum += (num | (num >> 1) | temp)
temp = temp << 1
count -= 1
end
temp = 1
end
# Display number of element
print(" Given n : ", n, "\n")
# Display calculate sum
print(" Result : ", sum, "\n")
end
end
def main()
task = BinaryBits.new()
# Test A
# n = 10
# --------------
# 7 111
# 13 1101
# 14 1110
# 25 11001
# 26 11010
# 28 11100
# 49 110001
# 50 110010
# 52 110100
# 56 111000
# [7 + 13 + 14 + 25 + 26 + 28 + 49 +
# 50 + 52 + 56] = 320
task.threeActiveBits(10)
# Test B
# num = 20
# --------------
# 7 111
# 13 1101
# 14 1110
# 25 11001
# 26 11010
# 28 11100
# 49 110001
# 50 110010
# 52 110100
# 56 111000
# 97 1100001
# 98 1100010
# 100 1100100
# 104 1101000
# 112 1110000
# 193 11000001
# 194 11000010
# 196 11000100
# 200 11001000
# 208 11010000
# [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
# 52 + 56 + 97 + 98 + 100 + 104 + 112 +
# 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20)
end
main()
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
/*
Scala Program
Sum of n numbers with exactly 3 set bits
*/
class BinaryBits()
{
def threeActiveBits(n: Int): Unit = {
var count: Int = n;
var temp: Int = 1;
var num: Int = 2;
var sum: Int = 0;
while (count > 0)
{
num = (num << 1);
while (temp < (num >> 1) && count > 0)
{
sum += (num | (num >> 1) | temp);
temp = temp << 1;
count -= 1;
}
temp = 1;
}
// Display number of element
println(" Given n : " + n);
// Display calculate sum
println(" Result : " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BinaryBits = new BinaryBits();
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
task.threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20);
}
}
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
/*
Swift 4 Program
Sum of n numbers with exactly 3 set bits
*/
class BinaryBits
{
func threeActiveBits(_ n: Int)
{
var count: Int = n;
var temp: Int = 1;
var num: Int = 2;
var sum: Int = 0;
while (count > 0)
{
num = (num << 1);
while (temp < (num >> 1) && count > 0)
{
sum += (num | (num >> 1) | temp);
temp = temp << 1;
count -= 1;
}
temp = 1;
}
// Display number of element
print(" Given n : ", n);
// Display calculate sum
print(" Result : ", sum);
}
}
func main()
{
let task: BinaryBits = BinaryBits();
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
task.threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20);
}
main();
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
/*
Kotlin Program
Sum of n numbers with exactly 3 set bits
*/
class BinaryBits
{
fun threeActiveBits(n: Int): Unit
{
var count: Int = n;
var temp: Int = 1;
var num: Int = 2;
var sum: Int = 0;
while (count > 0)
{
num = (num shl 1);
while (temp < (num shr 1) && count > 0)
{
sum += (num or(num shr 1) or temp);
temp = temp shl 1;
count -= 1;
}
temp = 1;
}
// Display number of element
println(" Given n : " + n);
// Display calculate sum
println(" Result : " + sum);
}
}
fun main(args: Array < String > ): Unit
{
val task: BinaryBits = BinaryBits();
// Test A
// n = 10
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 +
// 50 + 52 + 56] = 320
task.threeActiveBits(10);
// Test B
// num = 20
// --------------
// 7 111
// 13 1101
// 14 1110
// 25 11001
// 26 11010
// 28 11100
// 49 110001
// 50 110010
// 52 110100
// 56 111000
// 97 1100001
// 98 1100010
// 100 1100100
// 104 1101000
// 112 1110000
// 193 11000001
// 194 11000010
// 196 11000100
// 200 11001000
// 208 11010000
// [7 + 13 + 14 + 25 + 26 + 28 + 49 + 50 +
// 52 + 56 + 97 + 98 + 100 + 104 + 112 +
// 193 + 194 + 196 + 200 + 208] = 1822
task.threeActiveBits(20);
}
Output
Given n : 10
Result : 320
Given n : 20
Result : 1822
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