标记一系列数字中的素数

时间:2021-02-28 11:24:17

what I'm trying to do is to print the numbers 2-100 and mark the prime numbers with -

我要做的是打印数字2-100并用 - 标记素数 -

So, this should be the output:

所以,这应该是输出:

2-
3-
4
5-
6
7-
8
9
10
.
.
.
100

1 个解决方案

#1


0  

Try this code:

试试这段代码:

primes = []

for candidate in range(2, 101):
    can_be_prime = True
    for prime in primes:
        if candidate % prime == 0:
            can_be_prime = False
            break
    if can_be_prime:
        primes.append(candidate)
        print "%d-" % candidate
    else:
        print candidate

#1


0  

Try this code:

试试这段代码:

primes = []

for candidate in range(2, 101):
    can_be_prime = True
    for prime in primes:
        if candidate % prime == 0:
            can_be_prime = False
            break
    if can_be_prime:
        primes.append(candidate)
        print "%d-" % candidate
    else:
        print candidate