Python Coding Test Course, Finding the Sum of Numbers

Hello! In this coding test lecture, we will tackle the problem of finding the sum of numbers using Python. This problem is a basic type that frequently appears in algorithm problem-solving, and it is useful for practicing basic input handling and calculations using loops.

Problem Description

Write a function sum_numbers(N) that calculates the sum of all integers from 1 to N for a given integer N. Additionally, you should prompt the user to input the value of N and print the corresponding sum.

Input

  • Integer N (1 ≤ N ≤ 10000)

Output

  • The sum of all integers from 1 to N

Example

Input Example

5

Output Example

15

Problem Solving Process

To solve this problem, we will go through the following steps.

1. Understanding the Problem

Let’s organize the approach to understand the problem. We need to compute the sum of integers from 1 to N, which can be done using loops or a mathematical formula. Using a loop allows us to add each number iteratively, while using a mathematical formula can yield a more efficient solution.

2. Mathematical Approach

The sum of integers from 1 to N can be calculated mathematically using the following formula:

Sum = N * (N + 1) / 2

This formula allows us to obtain the result with O(1) time complexity instead of summing all integers.

3. Implementing Python Code

Now, let’s implement this problem in Python. We will implement it in two ways: using a loop and using a mathematical formula.

Method 1: Using a Loop


def sum_numbers_loop(N):
    total = 0
    for i in range(1, N + 1):
        total += i
    return total

# Get user input
N = int(input("Please enter an integer (1 ≤ N ≤ 10000): "))
result = sum_numbers_loop(N)
print("The sum from 1 to", N, "is:", result)

Method 2: Using a Mathematical Formula


def sum_numbers_formula(N):
    return N * (N + 1) // 2

# Get user input
N = int(input("Please enter an integer (1 ≤ N ≤ 10000): "))
result = sum_numbers_formula(N)
print("The sum from 1 to", N, "is:", result)

4. Testing and Exception Handling

To verify that our functions work correctly, we will create several test cases. For example, we can test the boundary values of N (1 and 10000) and some general values.

Test Cases

  • Input: 1, Output: 1
  • Input: 5, Output: 15 (1+2+3+4+5)
  • Input: 10000, Output: 50005000 (1+2+…+10000)

In addition to these, we can ensure the stability of the function through a variety of inputs.

5. Complexity Analysis

The time complexity for each method is as follows:

  • Using a loop: O(N) – Performance may degrade as N increases.
  • Using a mathematical formula: O(1) – Much faster and more efficient.

Conclusion

In this lecture, we explored two methods to solve the problem of finding the sum of numbers in Python. While we can solve the problem using a loop, we learned that utilizing a mathematical formula is far more efficient. Through these basic problems, we can build our coding foundations and prepare to tackle more complex challenges.

Additional Learning Resources

  • Baekjoon Online Judge – A platform to solve various algorithm problems
  • Programmers – Algorithm problems and solutions for interview preparation
  • Codewars – A website where you can build your skills by solving problems

Thank you! We hope you continue to build your skills through a variety of algorithm problems. See you in the next lecture!