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

📅  最后修改于: 2021-04-23 06:25:54             🧑  作者: 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