Cách kiểm tra số tự nhiên trong python

Mã ví dụ Python này trình bày một chương trình Python đơn giản để tìm tổng các số tự nhiên và in kết quả ra màn hình. Chương trình này lấy một số nguyên đầu vào từ người dùng và chạy vòng lặp từ số đã lấy về 0 và thêm các số trong vòng lặp

Chương trình

#Taking user input
num = int[input['Enter a number:']]

if num < 0:
   num = input['Please enter a positive number:']
else:
   sum = 0
   #Loop to iterate till zero
   while[num > 0]:
       sum += num
       num -= 1
   print["The sum of the natural numbers is:", sum]

Đầu ra chương trình

Enter a number:15
The sum of the natural numbers is: 120
báo cáo quảng cáo này

Chương trình Python cho số tự nhiên này giống như ví dụ đầu tiên. Nhưng lần này, chúng tôi cho phép người dùng nhập các giá trị tối thiểu và tối đa. Nó có nghĩa là chương trình này in các số tự nhiên từ tối thiểu đến tối đa

Đếm các số như 1, 2, 3, 4, 5, 6… Về cơ bản, mọi số nguyên lớn hơn 0 đều là số tự nhiên.  

Sự thật về số tự nhiên

  1. Chúng là những số nguyên [gọi là số nguyên], và không bao giờ nhỏ hơn 0 [i. e. số dương]
  2. Số tự nhiên có thể tiếp theo có thể được tìm thấy bằng cách thêm 1 vào số tự nhiên hiện tại
  3. Các số tự nhiên là các số bình thường, 1, 2, 3, v.v. , mà chúng tôi đếm
  4. Số 0 đôi khi được coi là số tự nhiên. Không phải lúc nào cũng vậy vì không ai tính bắt đầu bằng 0, 0, 1, 2, 3
  5. GCD của tất cả các số tự nhiên khác có số nguyên tố luôn là một
  6. Các số tự nhiên có thể được định nghĩa chính thức bằng cách liên hệ chúng với các tập hợp. Khi đó, số 0 là số phần tử trong tập rỗng;

Làm cách nào để in tổng n số tự nhiên?

Sử dụng đệ quy

Cho số n, tìm tổng n số tự nhiên đầu tiên. Để tính tổng, chúng ta sẽ sử dụng hàm đệ quy recur_sum[]

ví dụ.   

Input : 3
Output : 6
Explanation : 1 + 2 + 3 = 6

Input : 5
Output : 15
Explanation : 1 + 2 + 3 + 4 + 5 = 15

C++




// C++ program to find the

// sum of natural numbers up

// to n using recursion

#include

using namespace

15 
0

 

15 
1

15 
2

15 
3
15 
4
15 
3
15 
6

15 
7

15 
8
15 
9
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
0

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
3

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
6

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

 

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
8

15 
3
15
0

15 
7

15 
8
15 
3
15
4

15 
8
15
6

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
15
9

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

Java




// C++ program to find the1

// sum of natural numbers up

// to n using recursion

// C++ program to find the4 // C++ program to find the5

// C++ program to find the4 // C++ program to find the7

 

// C++ program to find the8 // C++ program to find the9

15 
7

 

15 
8
15 
1

15 
8
15 
2

15 
8// sum of natural numbers up6 // sum of natural numbers up7
15 
3
15 
4
15 
3
15 
6

15 
8
15 
7

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
15 
9 // to n using recursion6// to n using recursion7// to n using recursion8

// to n using recursion9

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
3

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2 #include 4// to n using recursion7#include 6

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

15 
8

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
8

15 
8// sum of natural numbers up6 // sum of natural numbers up7 using5 using6

15 
8
15 
7

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
15 
3 namespace1namespace2namespace3

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1namespace5

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

con trăn




namespace9

15 
00

15 
01

 

15 
02

15 
03

15 
04
15 
05

15 
8
15 
9
15 
08
15 
09 // to n using recursion7
15 
11

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
15 
14

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
15 
14_______3_______18
15 
19
15 
20 // to n using recursion7// to n using recursion8

 

15 
23

15 
14
15 
09 namespace2

15 
27______3_______28

C#




15 
29

15 
30

15 
31

using

15 
33

 

// C++ program to find the8 // C++ program to find the9

15 
7

 

15 
8
15 
1

15 
8
15 
2

15 
8// sum of natural numbers up6 // sum of natural numbers up7
15 
3
15 
4
15 
3
15 
6

15 
8
15 
7

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1____3_______9
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
0

// to n using recursion9

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
3

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
6

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

15 
8

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
8

15 
8// sum of natural numbers up6 // sum of natural numbers up7 using5
15 
68

15 
8
15 
7

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
15 
3
15
4

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1____3_______75

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

PHP




15 
79

15 
80

15 
30

15 
31

 

15 
1

15 
2

15 
85
15 
4
15 
87// to n using recursion8

15 
7

15 
8
15 
9
15 
92
15 
87
15 
94

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
15 
87namespace3

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
15 
87
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
02
15 
87
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
04

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

 

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
8

15 
87
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
08

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
09_______13_______10
15 
87
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
12

 

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
13

Javascript




1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
14

 

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
15

15 
30

15 
31

 

15 
1

15 
2

15 
85
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
21

15 
7

15 
8
15 
9
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
0

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
1
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
3

15 
8
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
2
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
6

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
7

 

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
8

15
4

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
35

 

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
36

đầu ra.  

15 

Thời gian phức tạp. Trên]

Không gian phụ trợ. Trên]

Sử dụng vòng lặp

Một giải pháp đơn giản là làm như sau.  

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  

C++




1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
37

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
38

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
39

using namespace

15 
0

 

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
43

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
44

15 
3
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
46
15 
3
15 
6

15 
7

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
50
15 
3
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
52

1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
50
1] Initialize : sum = 0
2] Run a loop from x = 1 to n and 
   do following in loop.
     sum = sum + x  
54
15 
92// C++ program to find the37 // C++ program to find the38// C++ program to find the37 // C++ program to find the40
15 
87namespace3// C++ program to find the37// C++ program to find the44

Chủ Đề