-->

whaust

2020年2月3日 星期一

print all Prime numbers in two integers Interval . (Python)

在兩個正整數之間印出所有質數。 (Python)

給定兩個正整數開始和結束。
任務是編寫一個Python程式印出兩個正整數間隔中的所有質數。


定義:

質數是大於1的自然數,除1及其本身外,沒有除數。
前幾個質數是{2、3、5、7、11…。}。

解決此問題的想法是使用for循環從頭到尾 迭代val,對於每個數字,如果它大於1,請檢查是否將n除盡。
如果找到其他除數,請打印該值。


Given two positive integer start and end. The task is to write a Python program to print all Prime numbers in an Interval.
Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.
The idea to solve this problem is to iterate the val from start to end using a for loop and for every number, if it is greater than 1, check if it divides n. If we find any other number which divides, print that value.



# -*- coding: utf-8 -*-

t = 0

numberStart = int(input("Enter a Start number: "))

numberEnd = int(input("Enter a End number: "))

for val in range(numberStart, numberEnd + 1): 

   if val > 1: 

       for n in range(2, val): 

           if (val % n) == 0: 

               break

       else: 

           print(val)

           t = t + 1

print("There are " , t , " Primes"  )



沒有留言:

張貼留言

Popular