正好设置了2位的数字之和

给定一个数字n。求所有n到n的数字之和,其中2位已设置。

null

例如:

Input : 10Output : 333 + 5 + 6 + 9 + 10 = 33Input : 100Output : 762

天真的方法: 找到每一个设置了2位的数字,最大为n。如果设置了2位,则将其添加到总和中。

C++

// CPP program to find sum of numbers
// upto n whose 2 bits are set
#include <bits/stdc++.h>
using namespace std;
// To count number of set bits
int countSetBits( int n)
{
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
// To calculate sum of numbers
int findSum( int n)
{
int sum = 0;
// To count sum of number
// whose 2 bit are set
for ( int i = 1; i <= n; i++)
if (countSetBits(i) == 2)
sum += i;
return sum;
}
// Driver program to test above function
int main()
{
int n = 10;
cout << findSum(n);
return 0;
}


JAVA

// Java program to find sum of numbers
// upto n whose 2 bits are set
public class Main {
// To count number of set bits
static int countSetBits( int n)
{
int count = 0 ;
while (n > 0 ) {
n &= (n - 1 );
count++;
}
return count;
}
// To calculate sum of numbers
static int findSum( int n)
{
int sum = 0 ;
// To count sum of number
// whose 2 bit are set
for ( int i = 1 ; i <= n; i++)
if (countSetBits(i) == 2 )
sum += i;
return sum;
}
// Driver program to test above function
public static void main(String[] args)
{
int n = 10 ;
System.out.println(findSum(n));
}
}


Python3

# Python program to find
# sum of numbers
# upto n whose 2 bits are set
# To count number of set bits
def countSetBits(n):
count = 0
while (n):
n = n & (n - 1 )
count = count + 1
return count
# To calculate sum of numbers
def findSum(n):
sum = 0
# To count sum of number
# whose 2 bit are set
for i in range ( 1 ,n + 1 ):
if (countSetBits(i) = = 2 ):
sum = sum + i
return sum
# Driver code
n = 10
print (findSum(n))
# This code is contributed
# by Anant Agarwal.


C#

// C# program to find sum of
// numbers upto n whose 2
// bits are set
using System;
class GFG
{
// To count number
// of set bits
static int countSetBits( int n)
{
int count = 0;
while (n > 0)
{
n = n & (n - 1);
count++;
}
return count;
}
// To calculate
// sum of numbers
static int findSum( int n)
{
int sum = 0;
// To count sum of number
// whose 2 bit are set
for ( int i = 1; i <= n; i++)
if (countSetBits(i) == 2)
sum += i;
return sum;
}
// Driver Code
static public void Main ()
{
int n = 10;
Console.WriteLine(findSum(n));
}
}
// This code is contributed by aj_36


PHP

<?php
// PHP program to find sum of numbers
// upto n whose 2 bits are set
// To count number of set bits
function countSetBits( $n )
{
$count = 0;
while ( $n )
{
$n &= ( $n - 1);
$count ++;
}
return $count ;
}
// To calculate sum of numbers
function findSum( $n )
{
$sum = 0;
// To count sum of number
// whose 2 bit are set
for ( $i = 1; $i <= $n ; $i ++)
if (countSetBits( $i ) == 2)
$sum += $i ;
return $sum ;
}
// Driver Code
$n = 10;
echo findSum( $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript program to find sum of numbers
// upto n whose 2 bits are set
// To count number of set bits
function countSetBits(n)
{
let count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
// To calculate sum of numbers
function findSum(n)
{
let sum = 0;
// To count sum of number
// whose 2 bit are set
for (let i = 1; i <= n; i++)
if (countSetBits(i) == 2)
sum += i;
return sum;
}
// Driver program to test above function
let n = 10;
document.write(findSum(n));
// This code is contributed by Mayank Tyagi
</script>


输出:

33

有效方法: 设置了2位的数字的形式是2^x+2^y,这个数字小于n。因此,我们必须只找到n范围内的数字,其形式是2^i+2^j,其中i>0,2^i

C++

// C++ program to find sum of numbers
// upto n whose 2 bits are set
#include <bits/stdc++.h>
using namespace std;
// To calculate sum of numbers
int findSum( int n)
{
int sum = 0;
// Find numbers whose 2 bits are set
for ( int i = 1; (1 << i) < n; i++) {
for ( int j = 0; j < i; j++) {
int num = (1 << i) + (1 << j);
// If number is greater then n
// we don't include this in sum
if (num <= n)
sum += num;
}
}
// Return sum of numbers
return sum;
}
// Driver program to test findSum()
int main()
{
int n = 10;
cout << findSum(n);
return 0;
}


JAVA

// Java program to find sum of numbers
// upto n whose 2 bits are set
public class Main {
// To calculate sum of numbers
static int findSum( int n)
{
int sum = 0 ;
// Find numbers whose 2 bits are set
for ( int i = 1 ; 1 << i < n; i++) {
for ( int j = 0 ; j < i; j++) {
int num = ( 1 << i) + ( 1 << j);
// If number is greater then n
// we don't include this in sum
if (num <= n)
sum += num;
}
}
// Return sum of numbers
return sum;
}
// Driver program to test findSum()
public static void main(String[] args)
{
int n = 10 ;
System.out.println(findSum(n));
}
}


Python3

# Python3 program to find sum of
# numbers upto n whose 2 bits are set
# To calculate sum of numbers
def findSum(n) :
sum = 0
# Find numbers whose 2
# bits are set
i = 1
while (( 1 << i) < n ) :
for j in range ( 0 , i) :
num = ( 1 << i) + ( 1 << j)
# If number is greater then n
# we don't include this in sum
if (num < = n) :
sum + = num
i + = 1
# Return sum of numbers
return sum
# Driver Code
n = 10
print (findSum(n))
# This code is contributed
# by Smitha


C#

// C# program to find sum of numbers
// upto n whose 2 bits are set
using System;
public class main {
// To calculate sum of numbers
static int findSum( int n)
{
int sum = 0;
// Find numbers whose 2 bits are set
for ( int i = 1; 1 << i < n; i++)
{
for ( int j = 0; j < i; j++)
{
int num = (1 << i) + (1 << j);
// If number is greater then n
// we don't include this in sum
if (num <= n)
sum += num;
}
}
// Return sum of numbers
return sum;
}
// Driver Code
public static void Main(String []args)
{
int n = 10;
Console.WriteLine(findSum(n));
}
}
// This Code is contributed by vt_m.


PHP

<?php
<?php
// PHP program to find sum of numbers
// upto n whose 2 bits are set
// To calculate sum of numbers
function findSum( $n )
{
$sum = 0;
// Find numbers whose 2 bits are set
for ( $i = 1; (1 << $i ) < $n ; $i ++)
{
for ( $j = 0; $j < $i ; $j ++)
{
$num = (1 << $i ) + (1 << $j );
// If number is greater then n
// we don't include this in sum
if ( $num <= $n )
$sum += $num ;
}
}
// Return sum of numbers
return $sum ;
}
// Driver Code
$n = 10;
echo findSum( $n );
// This code is contributed by Ajit
?>


Javascript

<script>
// Javascript program to find sum of numbers
// upto n whose 2 bits are set
// To calculate sum of numbers
function findSum(n)
{
let sum = 0;
// Find numbers whose 2 bits are set
for (let i = 1; 1 << i < n; i++)
{
for (let j = 0; j < i; j++)
{
let num = (1 << i) + (1 << j);
// If number is greater then n
// we don't include this in sum
if (num <= n)
sum += num;
}
}
// Return sum of numbers
return sum;
}
let n = 10;
document.write(findSum(n));
</script>


输出:

33

本文由 努克洛德 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享