Sum of first N Star Numbers

Given a number N, the task is to find the sum of the first N star numbers.
The first few star numbers are 1, 13, 37, 73,..

Examples:

Input: N = 2
Output: 14
Explanation:
1, 13 are the first two star numbers.

Input: N = 3
Output: 51

Approach 1:



  1. Nth Star number is given as 6*n^2 - 6*n + 1
  2. Run a loop from 1 to N, to find first N star numbers.
  3. Add all the above calculated star numbers.
  4. Return the sum.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the sum of 
// the first N Star Number
#include <bits/stdc++.h> 
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____ 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Output:

51

Time complexity: O(N).

Efficient Approach:

  • We already know \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 and \sum 1 = n
  • Nth star number is given as 6*n^2 - 6*n + 1
  • So, the sum of first N Star Numbers is \sum 6*n^2 - 6*n + 1
    Sum =  6*\sum n^2 - 6*\sum n + \sum 1
    Sum =  6*\frac{n*(n+1)*(2n+1)}{6} - 6*\frac{n*(n+1)}{2} + n
    Sum =  2*n*(n+1)*(n-1) + n
  • Calculate the sum and return.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the 
// sum of the first N 
// star numbers
#include <bits/stdc++.h>
  
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 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Output:

51

Time complexity: O(1).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.