How to print only the last line of this output?

652 Views Asked by At

I need to find the longest row of non primes below 10000.

How to print only that last line that's in my output ? Without max function and without simple numerical constraint.

I think it only needs some small adjustment, just don't see where or how.

priemen = []
for x in range(2,10000):    #prime generator
    tel = 0
    for deler in range(1,x):
        if x % deler == 0:
            tel += x % deler == 0
    if tel <2:
        priemen.append(x)   

a = priemen[0]
b = priemen[1]
maxrow = 0

for next in priemen[2:]:    
    a = b
    b = next
    row = b - a - 1       
    
    if row > maxrow:
        maxrow = row
        print("The longest row starts at", a+1, "and stops at", b-1, "and is", maxrow, "long.")

------------------
Output: 
The longest row starts at 8 and stops at 10 and is 3 long.
The longest row starts at 24 and stops at 28 and is 5 long.
The longest row starts at 90 and stops at 96 and is 7 long.
The longest row starts at 114 and stops at 126 and is 13 long.
The longest row starts at 524 and stops at 540 and is 17 long.
The longest row starts at 888 and stops at 906 and is 19 long.
The longest row starts at 1130 and stops at 1150 and is 21 long.
The longest row starts at 1328 and stops at 1360 and is 33 long.
The longest row starts at 9552 and stops at 9586 and is 35 long.

I only need it to print the last one

Thanks!!

2

There are 2 best solutions below

0
On BEST ANSWER

You need to save the values of a and b into separate variables so that you can print them after the loop.

b = priemen[1]
maxrow = 0

for n in priemen[2:]:
    a = b
    b = n
    row = b - a - 1       
    
    if row > maxrow:
        maxrow = row
        a_max = a
        b_max = b

if maxrow != 0:
    print("The longest row starts at", a_max + 1, "and stops at", b_max - 1, "and is", maxrow, "long.")

Other things to note:

  • I haven't initialised a_max and b_max - but the final if test is to guard against any situation where they have not yet been set
  • I have renamed next as n, because next is name of a builtin
  • the a = priemen[0] line is pointless so I have removed it
2
On

I see inefficiencies and problems with this code. First, it's inefficient in that it tests divisors from 1 to x

for deler in range(1, x):

When it only needs to test the odd divisors (after dealing with even numbers) from 3 to the square root of x. But even that would be inefficient in light of the fact it's creating a list of primes which it could instead use as the divisors to speed things up even more! Finally, efficiency-wise, I believe it can be done in a single pass:

TARGET = 10_000  # below this number

primes = [2]

start = end = primes[-1]

for number in range(3, TARGET, 2):

    def is_prime(number):
        for divisor in primes:
            if divisor * divisor > number:
                return True

            if number % divisor == 0:
                return False

        return True

    if is_prime(number):
        primes.append(number)

        if primes[-1] - primes[-2] > end - start:
            start, end = primes[-2:]

print("The longest run starts at", start + 1, "and stops at", end - 1, "and is", end - start - 1, "long.\n")

Finally, the problem is underspecified, and solution potentially wrong, as far as the target. Consider a target of 9586 instead of 10000. The code as written would print:

The longest run starts at 1328 and stops at 1360 and is 33 long.

But by adding the following code after the main loop:

if TARGET - primes[-1] > end - start:
    start, end = primes[-1], TARGET

We get the correct answer:

The longest run starts at 9552 and stops at 9585 and is 34 long.

The run would be longer if the target were greater, but it's still the longest run.