📜  不是 K 的幂的前 N 个自然数的总和

📅  最后修改于: 2021-10-26 05:32:58             🧑  作者: Mango

给定两个整数n  k  , 任务是找到范围[1, n]内所有数字的总和,不包括k 的正幂的数字即数字k, k 2 , k 3
例子:

方法:

  • 存储第一个的总和n  变量中的自然数sum  sum = (n * (n + 1)) / 2
  • 现在对于每一个积极的力量k  小于n  ,从sum  .
  • 打印变量的值sum  到底。

下面是上述方法的实现:

C++
// C++ program to find the sum of
// first n natural numbers which are
// not positive powers of k
#include 
using namespace std;
 
// Function to return the sum of
// first n natural numbers which are
// not positive powers of k
int find_sum(int n, int k)
{
    // sum of first n natural numbers
    int total_sum = (n * (n + 1)) / 2;
 
    int power = k;
    while (power <= n) {
 
        // subtract all positive powers
        // of k which are less than n
        total_sum -= power;
 
        // next power of k
        power *= k;
    }
 
    return total_sum;
}
 
// Driver code
int main()
{
    int n = 11, k = 2;
 
    cout << find_sum(n, k);
 
    return 0;
}


Java
// Java program to find the sum of
// first n natural numbers which are
// not positive powers of k
import java.io.*;
 
class GFG {
   
 
 
// Function to return the sum of
// first n natural numbers which are
// not positive powers of k
static int find_sum(int n, int k)
{
    // sum of first n natural numbers
    int total_sum = (n * (n + 1)) / 2;
 
    int power = k;
    while (power <= n) {
 
        // subtract all positive powers
        // of k which are less than n
        total_sum -= power;
 
        // next power of k
        power *= k;
    }
 
    return total_sum;
}
 
// Driver code
 
    public static void main (String[] args) {
        int n = 11, k = 2;
 
    System.out.println(find_sum(n, k));
    }
}
// This code is contributed by inder_verma..


Python3
# Python 3 program to find the sum of
# first n natural numbers which are
# not positive powers of k
 
# Function to return the sum of
# first n natural numbers which are
# not positive powers of k
def find_sum(n, k):
 
    # sum of first n natural numbers
    total_sum = (n * (n + 1)) // 2
    power = k
    while power <= n:
 
        # subtract all positive powers
        # of k which are less than n
        total_sum -= power
 
        # next power of k
        power *= k
    return total_sum
 
# Driver code
n = 11; k = 2
print(find_sum(n, k))
 
# This code is contributed
# by Shrikant13


C#
// C# program to find the sum of
// first n natural numbers which are
// not positive powers of k
using System;
 
class GFG {
 
 
 
// Function to return the sum of
// first n natural numbers which are
// not positive powers of k
static int find_sum(int n, int k)
{
    // sum of first n natural numbers
    int total_sum = (n * (n + 1)) / 2;
 
    int power = k;
    while (power <= n) {
 
        // subtract all positive powers
        // of k which are less than n
        total_sum -= power;
 
        // next power of k
        power *= k;
    }
 
    return total_sum;
}
 
// Driver code
 
    public static void Main () {
        int n = 11, k = 2;
 
    Console.WriteLine(find_sum(n, k));
    }
}
// This code is contributed by inder_verma..


PHP


Javascript


输出:
52