📜  前N个星号的总和

📅  最后修改于: 2021-04-21 21:53:52             🧑  作者: Mango

给定数字N ,任务是找到前N个星星的总和。
前几个星号是1、13、37、73 ..
例子:

方法1:

  1. N星号为6*n^2 - 6*n + 1
  2. 从1到N循环运行,以查找前N个星号。
  3. 将所有以上计算出的星号相加。
  4. 返回总和。

下面是上述方法的实现:

C++
// C++ program to find the sum of
// the first N Star Number
#include 
using namespace std;
 
// Function to find the N-th
// Star Number
int star_num(int n)
{
     
    // Formula to calculate nth
    // Star Number
    return (6 * n * n - 6 * n + 1);
}
 
// Function to find the sum of the
// first N Star Number
int sum_star_num(int n)
{
     
    // Variable to store the sum
    int summ = 0;
     
    // Iterating from 1 to N
    for(int i = 1; i < n + 1; i++)
    {
         
        // Finding the sum
        summ += star_num(i);
    }
    return summ;
}
 
// Driver code
int main()
{
    int n = 3;
     
    cout << sum_star_num(n);
}
 
// This code is contributed by spp____


Java
// Java program to find the sum of
// the first N Star Number
class GFG{
 
// Function to find the N-th
// Star Number
static int star_num(int n)
{
     
    // Formula to calculate nth
    // Star Number
    return (6 * n * n - 6 * n + 1);
}
 
// Function to find the sum of the
// first N Star Number
static int sum_star_num(int n)
{
     
    // Variable to store the sum
    int summ = 0;
     
    // Iterating from 1 to N
    for(int i = 1; i < n + 1; i++)
    {
         
        // Finding the sum
        summ += star_num(i);
    }
    return summ;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
     
    System.out.println(sum_star_num(n));
}
}
 
// This code is contributed by rock_cool


Python3
# Python3 program to find the
# sum of the first N 
# star numbers
 
# Function to find the
# N-th star
# number
def star_num(n):
  
    # Formula to calculate 
    # nth star
    # number
    return (6 * n * n - 6 * n + 1)
    
# Function to find the sum of
# the first N star numbers
def sum_star_num(n) :
     
    # Variable to store
    # the sum
    summ = 0
     
    # Iterating in the range
    # 1 to N
    for i in range(1, n + 1):
        summ += star_num(i)
     
    return summ
   
# Driver code
n = 3
print(sum_star_num(n))


C#
// C# program to find the sum of
// the first N Star Number
using System;
class GFG{
 
// Function to find the N-th
// Star Number
static int star_num(int n)
{
     
    // Formula to calculate nth
    // Star Number
    return (6 * n * n - 6 * n + 1);
}
 
// Function to find the sum of the
// first N Star Number
static int sum_star_num(int n)
{
     
    // Variable to store the sum
    int summ = 0;
     
    // Iterating from 1 to N
    for(int i = 1; i < n + 1; i++)
    {
         
        // Finding the sum
        summ += star_num(i);
    }
    return summ;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
     
    Console.WriteLine(sum_star_num(n));
}
}
 
// This code is contributed by gauravrajput1


Javascript


C++
// C++ program to find the
// sum of the first N
// star numbers
#include 
 
using namespace std;
 
// Function to find the
// sum of the first N
// star number
int sum_star_num(int n)
{
     
    // Variable to store
    // the sum
    int summ = 2 * n * (n + 1) * (n - 1) + n;
     
    return summ;
}
 
// Driver code
int main()
{
    int n = 3;
     
    cout << sum_star_num(n);
    return 0;
}
 
// This code is contributed by Amit Katiyar


Java
// Java program to find the
// sum of the first N 
// star numbers
class GFG{
     
    // Function to find the
    // sum of the first N
    // star number
    static int sum_star_num(int n)
    {
 
        // Variable to store
        // the sum
        int summ = 2 * n * (n + 1) * (n - 1) + n;
 
        return summ;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        System.out.println(sum_star_num(n));
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to find the
# sum of the first N 
# star numbers
 
# Function to find the
# sum of the first N
# star number
def sum_star_num(n) :
     
    # Variable to store
    # the sum
    summ = 2 * n*(n + 1)*(n-1) + n
     
    return summ
   
# Driver code
n = 3
print(sum_star_num(n))


C#
// C# program to find the
// sum of the first N
// star numbers
using System;
 
class GFG{
     
// Function to find the
// sum of the first N
// star number
static int sum_star_num(int n)
{
 
    // Variable to store
    // the sum
    int summ = 2 * n * (n + 1) * (n - 1) + n;
 
    return summ;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
     
    Console.WriteLine(sum_star_num(n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
51

时间复杂度: O(N)。
高效方法:

  • 我们已经知道\sum n = \frac{n*(n+1)}{2}     \sum n^2 = \frac{n*(n+1)*(2n+1)}{6}     \sum n^3 = \frac{n*(n+1)}{2}^2     \sum 1 = n
  • N星号为6*n^2 - 6*n + 1
  • 因此,前N个星号的总和为\sum 6*n^2 - 6*n + 1
    总和= 6*\sum n^2 - 6*\sum n + \sum 1
    总和= 6*\frac{n*(n+1)*(2n+1)}{6} - 6*\frac{n*(n+1)}{2} + n
    总和= 2*n*(n+1)*(n-1) + n
  • 计算总和并返回。

下面是上述方法的实现:

C++

// C++ program to find the
// sum of the first N
// star numbers
#include 
 
using namespace std;
 
// Function to find the
// sum of the first N
// star number
int sum_star_num(int n)
{
     
    // Variable to store
    // the sum
    int summ = 2 * n * (n + 1) * (n - 1) + n;
     
    return summ;
}
 
// Driver code
int main()
{
    int n = 3;
     
    cout << sum_star_num(n);
    return 0;
}
 
// This code is contributed by Amit Katiyar

Java

// Java program to find the
// sum of the first N 
// star numbers
class GFG{
     
    // Function to find the
    // sum of the first N
    // star number
    static int sum_star_num(int n)
    {
 
        // Variable to store
        // the sum
        int summ = 2 * n * (n + 1) * (n - 1) + n;
 
        return summ;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        System.out.println(sum_star_num(n));
    }
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 program to find the
# sum of the first N 
# star numbers
 
# Function to find the
# sum of the first N
# star number
def sum_star_num(n) :
     
    # Variable to store
    # the sum
    summ = 2 * n*(n + 1)*(n-1) + n
     
    return summ
   
# Driver code
n = 3
print(sum_star_num(n))

C#

// C# program to find the
// sum of the first N
// star numbers
using System;
 
class GFG{
     
// Function to find the
// sum of the first N
// star number
static int sum_star_num(int n)
{
 
    // Variable to store
    // the sum
    int summ = 2 * n * (n + 1) * (n - 1) + n;
 
    return summ;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
     
    Console.WriteLine(sum_star_num(n));
}
}
 
// This code is contributed by PrinciRaj1992

Java脚本


输出:
51

时间复杂度: O(1)。