Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Find Cube of a Number - Python
Next article icon

Python Program for cube sum of first n natural numbers

Last Updated : 24 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a number n and we need to print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till the n-th term in python.

Examples:

Input: n = 5
Output: 225
Explanation: 13 + 23 + 33 + 43 + 53 = 225

Let’s discuss some of the ways to do it.

Using Mathematical Formula:

Most efficient solution is to use the direct mathematical formula which is (n ( n + 1 ) / 2) ^ 2, where n is the number of terms.

Python
n = 5 res = ((n * (n + 1)) // 2) ** 2 print(res) 

Output
225 

Explanation: ((n * (n + 1)) // 2) ** 2 gives us the sum of cubes of integers from 1 to n.

Table of Content

  • Using Brute Force approach
  • Using list comprehension
  • Using Enumerate List

Using Brute Force approach

We can iterate from 1 to n, computing the cube of each number and summing them, where n is the limit up to which the sum is required.

Python
n = 5 sum = 0  for i in range(1, n + 1):     res += i ** 3  print(res) 

Output
225 

Explanation: iterate from 1 to n using for loop and sum += i**3, keeps on adding the the cubes of the numbers.

Using generator expression

Use generator expression to generate a list of cubes for numbers from 1 to n, then apply the sum() function to get the total. This approach is concise and efficient, combining iteration and summation in a single line.

Python
n = 5  res = sum(i**3 for i in range(1, n + 1))  print(res) 

Output
225 

Explanation: i**3 for i in range(1, n + 1) creates a list of cubes of numbers from 1 to n and then sum() function returns the sum of all the elements inside the list.

Using Enumerate List

In this approach, we will use the enumerate list to find the cube sum of n natural numbers in one line.

Python
n = 5 res = sum([(i+1) ** 3 for i, _ in enumerate(range(n))]) print(res) 

Output
225 

Explanation:

  • use list comprehension with enumerate(range(n)) to generate cubes of numbers from 1 to n.
  • apply the sum() function to compute the total sum of cubes.
  • underscore (_) in enumerate() is a throwaway variable since enumerate(range(n)) returns both index and value, but only the index (i) is used.


Next Article
Find Cube of a Number - Python
author
kartik
Improve
Article Tags :
  • DSA
  • Python Programs
  • maths-cube

Similar Reads

  • Sum of squares of first N natural numbers - Python
    The goal here is to calculate the sum of squares of the first n natural numbers. The sum of squares is the result of adding up the squares of each number from 1 to n. For example, if n = 5, the sum of squares would be 12 + 22 + 32+ 42+52=55. Let's explore different methods to compute this sum effici
    3 min read
  • Python Program to Find Cube of a Number
    We are given a number and our task is to find the cube of this number in Python. The cube of a number is calculated by multiplying the number by itself twice. For example, if the input is 3, then the output will be 3 * 3 * 3 = 27. In this article, we will learn different ways to find the cube of a n
    2 min read
  • Python Program to Get Sum of N Armstrong Number
    Given a number N, determine the sum of the first N Armstrong numbers using Python. Example: Input : 11Output : 568First 11 Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, lies to, 370Their summation is 578Method 1: Using Iterative methodsCreate a while loop that breaks when the desired number of Ar
    3 min read
  • Python Program to Find the Sum of Natural Numbers Using While Loop
    Calculating the Sum of N numbers in Python using while loops is very easy. In this article, we will understand how we can calculate the sum of N numbers in Python using while loop. Calculate Sum of Natural Numbers in Python Using While LoopBelow are some of the examples by which we can see how we ca
    2 min read
  • Python program to find Cumulative sum of a list
    Calculating the cumulative sum of a list means finding the running total of the elements as we move through the list. In this article, we will explore How to find the cumulative sum of a list. Using itertools.accumulate()This is the most efficient method for calculating cumulative sums. itertools mo
    3 min read
  • Python Program to calculate sum and average of three numbers
    We are given three numbers, and our task is to calculate the sum and average of these numbers. The average of the numbers is defined as the sum of the given numbers divided by the total number of elements. In this case, it will be the sum of given three numbers divided by 3. Example: Input: 10, 20,
    3 min read
  • Python Program to Get Sum of cubes of alternate even numbers in an array
    Given an array, write a program to find the sum of cubes of alternative even numbers in an array. Examples: Input : arr = {1, 2, 3, 4, 5, 6}Output : Even elements in given array are2,4,6Sum of cube of alternate even numbers are 2**3+6**3 = 224Input : arr = {1,3,5,8,10,9,11,12,1,14}Output : Even elem
    5 min read
  • Python Program to Check Armstrong Number
    Given a number x, determine whether the given number is Armstrong number or not. A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if. abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... Example: Input : 153 Output : Yes 153 is an Armstrong nu
    6 min read
  • Python Program For Finding Subarray With Given Sum - Set 1 (Nonnegative Numbers)
    Given an unsorted array of nonnegative integers, find a continuous subarray which adds to a given number. Examples :  Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Output: Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input: arr[] = {1, 4, 0, 0, 3, 10,
    5 min read
  • Python Program to Find Sum of First and Last Digit
    Given a positive integer N(at least contain two digits). The task is to write a Python program to add the first and last digits of the given number N. Examples: Input: N = 1247 Output: 8 Explanation: First digit is 1 and Last digit is 7. So, addition of these two (1 + 7) is equal to 8.Input: N = 73
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences