给定两个整数N和K ,任务是从范围[1,N]中查找所有数字的和,不包括K的幂。
例子:
Input: N = 10, K = 3
Output: 42
2 + 4 + 5 + 6 + 7 + 8 + 10 = 42
1, 3 and 9 are excluded as they are powers of 3.
Input: N = 200, K = 30
Output: 20069
方法:找到以下系列的总和:
- pwrK:K个的所有权力从[1,N]即ķ0 + K 1 + K 2 + … + K R,如的Kr≤n中的总和
- sumAll:范围为[1,N]的所有整数的总和,即(N *(N +1))/ 2 。
结果将是sumAll – pwrK
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long int
// Function to return the sum of all the
// powers of k from the range [1, n]
ll sumPowersK(ll n, ll k)
{
// To store the sum of the series
ll sum = 0, num = 1;
// While current power of k <= n
while (num <= n) {
// Add current power to the sum
sum += num;
// Next power of k
num *= k;
}
// Return the sum of the series
return sum;
}
// Find to return the sum of the
// elements from the range [1, n]
// excluding those which are powers of k
ll getSum(ll n, ll k)
{
// Sum of all the powers of k from [1, n]
ll pwrK = sumPowersK(n, k);
// Sum of all the elements from [1, n]
ll sumAll = (n * (n + 1)) / 2;
// Return the required sum
return (sumAll - pwrK);
}
// Driver code
int main()
{
ll n = 10, k = 3;
cout << getSum(n, k);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG
{
// Function to return the sum of all the
// powers of k from the range [1, n]
static long sumPowersK(long n, long k)
{
// To store the sum of the series
long sum = 0, num = 1;
// While current power of k <= n
while (num <= n)
{
// Add current power to the sum
sum += num;
// Next power of k
num *= k;
}
// Return the sum of the series
return sum;
}
// Find to return the sum of the
// elements from the range [1, n]
// excluding those which are powers of k
static long getSum(long n, long k)
{
// Sum of all the powers of k from [1, n]
long pwrK = sumPowersK(n, k);
// Sum of all the elements from [1, n]
long sumAll = (n * (n + 1)) / 2;
// Return the required sum
return (sumAll - pwrK);
}
// Driver code
public static void main (String[] args)
{
long n = 10, k = 3;
System.out.println( getSum(n, k));
}
}
// This code is contributed by anuj_67..
Python3
# Python3 implementation of the approach
# Function to return the sum of all the
# powers of k from the range [1, n]
def sumPowersK(n, k) :
# To store the sum of the series
sum = 0; num = 1;
# While current power of k <= n
while (num <= n) :
# Add current power to the sum
sum += num;
# Next power of k
num *= k;
# Return the sum of the series
return sum;
# Find to return the sum of the
# elements from the range [1, n]
# excluding those which are powers of k
def getSum(n, k) :
# Sum of all the powers of k from [1, n]
pwrK = sumPowersK(n, k);
# Sum of all the elements from [1, n]
sumAll = (n * (n + 1)) / 2;
# Return the required sum
return (sumAll - pwrK);
# Driver code
if __name__ == "__main__" :
n = 10; k = 3;
print(getSum(n, k));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the sum of all the
// powers of k from the range [1, n]
static long sumPowersK(long n, long k)
{
// To store the sum of the series
long sum = 0, num = 1;
// While current power of k <= n
while (num <= n)
{
// Add current power to the sum
sum += num;
// Next power of k
num *= k;
}
// Return the sum of the series
return sum;
}
// Find to return the sum of the
// elements from the range [1, n]
// excluding those which are powers of k
static long getSum(long n, long k)
{
// Sum of all the powers of k from [1, n]
long pwrK = sumPowersK(n, k);
// Sum of all the elements from [1, n]
long sumAll = (n * (n + 1)) / 2;
// Return the required sum
return (sumAll - pwrK);
}
// Driver code
public static void Main ()
{
long n = 10, k = 3;
Console.WriteLine( getSum(n, k));
}
}
// This code is contributed by anuj_67..
Javascript
输出:
42
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。