素数是两个给定正整数之间的质数。

huangapple go评论69阅读模式
英文:

Prime numbers between two given positive integers

问题

Oh, I see some code here! Let me try to help you with the translation. Here's the translated code part:

这是用于在特定范围内查找素数的程序。
而不是获得素数,我得到了素数反复出现多次的情况。

这是用于在特定范围内查找素数的程序。

start = 25
end = 50

print(f"从{start}{end}的素数是:")

for num in range(start, end+1):
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                break
        else:
            print(num)

期望输出:

25到50之间的素数是:
29
31
37
41
43
47

I hope this helps, and if you have any more questions, just ask! 😄

英文:

This is the program for finding prime numbers in a specific range.
Instead of getting prime numbers, I am getting the prime numbers repeating itself for a number of times.

This is a program for finding prime numbers in a specific range.

start = 25
end = 50

print(f"The prime numbers from {start} to {end} are :  ")

for num in range(start, end+1):
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                break
            else:
                print(num)

Expected output:

Prime numbers between 25 and 50 are:
29
31
37
41
43
47

答案1

得分: 1

这是因为,在for i in range(2, num)下的if else块会执行多次,直到for循环跳出。

每当if条件计算为假时,else块都会执行,而这会发生多次。

因此,相同的数字会多次打印。

要纠正这个问题,您需要将打印语句移到for循环之外的某个地方。

英文:

This is because, the if else block under for i in range(2, num) is executed multiple times until the for loop breaks out.

Every time the if condition is evaluated to false, the else block is executed, and that happens multiple times

And therefore the same number is printed multiple times

To rectify this, you will have to move the print statement somewhere out of the for loop

start = 25
end = 50

print(f"The prime numbers from {start} to {end} are :  ")

for num in range(start, end+1):
    if num > 1:
        is_prime = True
        for i in range(2, num):
            if (num % i) == 0:
                is_prime = False
                break

        if is_prime:
            print(num)

答案2

得分: 0

编程的关键之一是将问题分解成较小的部分。尽管你计算质数的方法效率不高,但确实是有效的。如果将这部分代码拆分为一个单独的函数,你的代码将更容易理解。

import math

def is_prime(n):
  for i in range(2, math.ceil(math.sqrt(n)) + 1):
    if n % i == 0:
      return False
  return True

在这之后做了这样的操作:

>>> print(
...   *(x 
...     for x in range(20, 51) 
...     if is_prime(x)), 
...   sep='\n', end='\n'
... )
23
29
31
37
41
43
47

任何与质数相关的工作可能都应该阅读关于埃拉托斯特尼筛法的信息。

英文:

One of the keys of programming is breaking things down into smaller problems. While your approach to calculating primes is inefficient, it can be effective. Your code would be easier to understand if this were broken out into a separate function.

import math

def is_prime(n):
  for i in range(2, math.ceil(math.sqrt(n)) + 1):
    if n % i == 0:
      return False
  return True

Having done this:

>>> print(
...   *(x 
...     for x in range(20, 51) 
...     if is_prime(x)), 
...   sep='\n', end='\n'
... )
23
29
31
37
41
43
47

Anyone working with primes should probably read about the Sieve of Eratosthenes.

答案3

得分: 0

Sure thing! Here's the translated code for you:

你可以对数字范围应用筛法或埃拉托斯特尼筛法

def primesBetween(a, b):
    sieve = [1] * (b - a + 1)
    p2, p, inc = 4, 2, 1
    while p2 <= b:
        start = (-a) % p if p2 < a else p2 - a
        sieve[start::p] = (len(sieve) - start + p - 1) // p * [0]
        p, inc = p + inc, 2
        p2 = p * p
    return [n for n, isPrime in enumerate(sieve, a) if isPrime]

output:

print(primesBetween(100, 200))
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
173, 179, 181, 191, 193, 197, 199]

Happy coding, friend! 🚀

英文:

You could apply the sieve or Eratosthenes to the range of numbers:

def primesBetween(a,b):
    sieve = [1]*(b-a+1)
    p2, p, inc = 4, 2, 1
    while p2 &lt;= b:
        start = (-a)%p if p2 &lt; a else p2-a 
        sieve[start::p]  = (len(sieve)-start+p-1)//p * [0]
        p,inc = p+inc, 2
        p2    = p * p
    return [n for n,isPrime in enumerate(sieve,a) if isPrime]

output:

print(primesBetween(100,200))
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 
 173, 179, 181, 191, 193, 197, 199]

huangapple
  • 本文由 发表于 2023年7月17日 10:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定