Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of summation of pyramid numbers in Python/ R language respectively

Shulou Source: shulou.com Published: 2022-06-01 03:51:26 09月18日 Update

This article introduces the relevant knowledge of "how to solve the pyramid number summation problem with Python/R language respectively". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

1. First N factorial summation

Factorial is a mathematical term invented by Christian Kramp (1760 - 1826) in 1808.

n!= 1∗2∗3∗...∗(n−1)∗n

Or you can define it recursively:

The sum of the factorial of its first N terms is sum(1!+ 2!+ 3!+...+ n!),With the above theoretical basis, let's look at the diagram together to strengthen our understanding!

1.1 graphical problem

As shown in the figure, if you want to output each value, you can embed the idea of a loop, and multiply the previous value every time!

1.2 algorithm flow

Here we can see that we need to manually control how many terms we need to count first. Inside the loop, this is the accumulation and assignment of factorial numbers.

1.3 code implementation

1.3.1 Python code implementation

n = int(input("n = "))s = 0t = 1for i in range(1,n+1): t*=i s+=tprint ("sum of previous {} factorial is: {}" .format(n,s))

1.3.2 R Language Code Implementation

factorial

Tags: Gold characters pyramids factorials codes problems loops inputs languages numbers algorithms experiments operations content functions loops numbers ways more processes knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB macOS Redmi Shulou Tech Info