📜  K 的最小值,使得前 K 个自然数的立方和大于等于 N

📅  最后修改于: 2021-09-16 11:03:45             🧑  作者: Mango

给定一个数N ,任务是找到最小值K使得前K 个自然数的立方和大于或等于N
例子:

朴素的方法:这个问题的朴素的方法是运行一个循环并找到立方体的总和。每当总和超过 N 的值时,就退出循环。
下面是上述方法的实现:

C++
// C++ program to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
#include 
using namespace std;
 
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
int naive_find_x(int N)
{
 
    // Variable to store the
    // sum of cubes
    int c = 0, i;
 
    // Loop to find the number
    // K
    for(i = 1; i < N; i++)
    {
        c += i * i * i;
             
        // If C is just greater then
        // N, then break the loop
        if (c >= N)
            break;
    }
    return i;
}
 
// Driver code
int main()
{
    int N = 100;
    cout << naive_find_x(N);
    return 0;
}
 
// This code is contributed by sapnasingh4991


Java
// Java program to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
class GFG {
     
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int naive_find_x(int N)
{
 
    // Variable to store the
    // sum of cubes
    int c = 0, i;
 
    // Loop to find the number
    // K
    for(i = 1; i < N; i++)
    {
       c += i * i * i;
        
       // If C is just greater then
       // N, then break the loop
       if (c >= N)
           break;
    }
    return i;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 100;
     
    System.out.println(naive_find_x(N));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to determine the
# minimum value of K such that the
# sum of cubes of first K
# natural number is greater than
# or equal to N
 
# Function to determine the
# minimum value of K such that the
# sum of cubes of first K
# natural number is greater than
# or equal to N
def naive_find_x(N):
 
    # Variable to store the
    # sum of cubes
    c = 0
 
    # Loop to find the number
    # K
    for i in range(1, N):
 
        c += i*i*i
 
        # If C is just greater then
        # N, then break the loop
        if c>= N:
            break
 
    return i
 
# Driver code
if __name__ == "__main__":
     
    N = 100
    print(naive_find_x(N))


C#
// C# program to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
using System;
 
class GFG {
     
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int naive_find_x(int N)
{
 
    // Variable to store the
    // sum of cubes
    int c = 0, i;
 
    // Loop to find the number
    // K
    for(i = 1; i < N; i++)
    {
    c += i * i * i;
         
    // If C is just greater then
    // N, then break the loop
    if (c >= N)
        break;
    }
    return i;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 100;
     
    Console.WriteLine(naive_find_x(N));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program to determine the
// minimum value of K such that
// the sum of cubes of first K
// natural number is greater than
// or equal to N
#include 
using namespace std;
     
// Function to determine the
// minimum value of K such that
// the sum of cubes of first K
// natural number is greater than
// or equal to N
int binary_searched_find_x(int k)
{
     
    // Left bound
    int l = 0;
 
    // Right bound
    int r = k;
 
    // Variable to store the
    // answer
    int ans = 0;
 
    // Applying binary search
    while (l <= r)
    {
         
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
 
        if (pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
             
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        }
        else
        {
 
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 100;
     
    cout << binary_searched_find_x(N);
    return 0;
}
 
// This code is contributed by shubhamsingh10


Java
// Java program to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
class GFG{
     
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int binary_searched_find_x(int k)
{
 
    // Left bound
    int l = 0;
 
    // Right bound
    int r = k;
 
    // Variable to store the
    // answer
    int ans = 0;
 
    // Applying binary search
    while (l <= r)
    {
 
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
 
        if (Math.pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
 
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        }
        else
        {
 
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 100;
    System.out.println(binary_searched_find_x(N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program to determine the
# minimum value of K such that the
# sum of cubes of first K
# natural number is greater than
# or equal to N
 
# Function to determine the
# minimum value of K such that the
# sum of cubes of first K
# natural number is greater than
# or equal to N
def binary_searched_find_x(k):
 
    # Left bound
    l = 0
 
    # Right bound
    r = k
 
    # Variable to store the
    # answer
    ans = 0
 
    # Applying binary search
    while l<= r:
 
        # Calculating mid value
        # of the range
        mid = l+(r-l)//2
 
        if ((mid*(mid + 1))//2)**2>= k:
 
             # If the sum of cubes of
             # first mid natural numbers
             # is greater than equal to N
             # iterate the left half
             ans = mid
             r = mid-1
 
        else:
 
             # Sum of cubes of first
             # mid natural numbers is
             # less than N, then move
             # to the right segment
             l = mid + 1    
 
    return ans   
 
# Driver code
if __name__ == "__main__":
    N = 100
    print(binary_searched_find_x(N))


C#
// C# program to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
using System;
class GFG{
     
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int binary_searched_find_x(int k)
{
 
    // Left bound
    int l = 0;
 
    // Right bound
    int r = k;
 
    // Variable to store the
    // answer
    int ans = 0;
 
    // Applying binary search
    while (l <= r)
    {
 
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
 
        if (Math.Pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
 
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        }
        else
        {
 
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int N = 100;
    Console.Write(binary_searched_find_x(N));
}
}
 
// This code is contributed by Nidhi_biet


Javascript


输出:

4

时间复杂度: O(K),其中 K 是需要找到的数字。
有效的方法:需要进行的一个观察是,前 N 个自然数的立方和由以下公式给出:

sum = ((N * (N + 1))/2)2

而且,这是一个单调递增的函数。因此,这个想法是应用二分搜索来找到 K 的值。如果某个数字 ‘i’ 的总和大于 N,那么我们知道答案小于或等于 ‘i’。所以,迭代到左半部分。否则,遍历右半部分。
下面是上述方法的实现:

C++

// C++ program to determine the
// minimum value of K such that
// the sum of cubes of first K
// natural number is greater than
// or equal to N
#include 
using namespace std;
     
// Function to determine the
// minimum value of K such that
// the sum of cubes of first K
// natural number is greater than
// or equal to N
int binary_searched_find_x(int k)
{
     
    // Left bound
    int l = 0;
 
    // Right bound
    int r = k;
 
    // Variable to store the
    // answer
    int ans = 0;
 
    // Applying binary search
    while (l <= r)
    {
         
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
 
        if (pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
             
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        }
        else
        {
 
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 100;
     
    cout << binary_searched_find_x(N);
    return 0;
}
 
// This code is contributed by shubhamsingh10

Java

// Java program to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
class GFG{
     
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int binary_searched_find_x(int k)
{
 
    // Left bound
    int l = 0;
 
    // Right bound
    int r = k;
 
    // Variable to store the
    // answer
    int ans = 0;
 
    // Applying binary search
    while (l <= r)
    {
 
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
 
        if (Math.pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
 
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        }
        else
        {
 
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 100;
    System.out.println(binary_searched_find_x(N));
}
}
 
// This code is contributed by 29AjayKumar

蟒蛇3

# Python program to determine the
# minimum value of K such that the
# sum of cubes of first K
# natural number is greater than
# or equal to N
 
# Function to determine the
# minimum value of K such that the
# sum of cubes of first K
# natural number is greater than
# or equal to N
def binary_searched_find_x(k):
 
    # Left bound
    l = 0
 
    # Right bound
    r = k
 
    # Variable to store the
    # answer
    ans = 0
 
    # Applying binary search
    while l<= r:
 
        # Calculating mid value
        # of the range
        mid = l+(r-l)//2
 
        if ((mid*(mid + 1))//2)**2>= k:
 
             # If the sum of cubes of
             # first mid natural numbers
             # is greater than equal to N
             # iterate the left half
             ans = mid
             r = mid-1
 
        else:
 
             # Sum of cubes of first
             # mid natural numbers is
             # less than N, then move
             # to the right segment
             l = mid + 1    
 
    return ans   
 
# Driver code
if __name__ == "__main__":
    N = 100
    print(binary_searched_find_x(N))

C#

// C# program to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
using System;
class GFG{
     
// Function to determine the
// minimum value of K such that the
// sum of cubes of first K
// natural number is greater than
// or equal to N
static int binary_searched_find_x(int k)
{
 
    // Left bound
    int l = 0;
 
    // Right bound
    int r = k;
 
    // Variable to store the
    // answer
    int ans = 0;
 
    // Applying binary search
    while (l <= r)
    {
 
        // Calculating mid value
        // of the range
        int mid = l + (r - l) / 2;
 
        if (Math.Pow(((mid * (mid + 1)) / 2), 2) >= k)
        {
 
            // If the sum of cubes of
            // first mid natural numbers
            // is greater than equal to N
            // iterate the left half
            ans = mid;
            r = mid - 1;
        }
        else
        {
 
            // Sum of cubes of first
            // mid natural numbers is
            // less than N, then move
            // to the right segment
            l = mid + 1;
        }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int N = 100;
    Console.Write(binary_searched_find_x(N));
}
}
 
// This code is contributed by Nidhi_biet

Javascript


输出:
4

时间复杂度: O(log(K))。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程