0
def step(g, m, n):
    notstorage=[]
    actualvalues =[]
    for i in range(m, (n+1)):
        for primechecker in range(2,i):
            if i % primechecker == 0:
                notstorage.append(i)



    for value in range(m, (n+1)):
        if (value) and (value+g) not in set(notstorage):
            actualvalues.append(value)
            actualvalues.append(value+g)
            break
    return(actualvalues)

In the above code I'm trying to figure out which numbers are prime and create a list that checks if there are two prime numbers separated by a certain number (g). I'm having issues because for some reason the number 303 is being returned as a prime number when it clearly is not since it is divisible by 3.... I'm not sure why the code isn't working properly?

When I input:

step(4,300,400)

I am expecting an output of (307,311) since those are the first 2 prime numbers that are 4 numbers apart but instead I get a return of (303, 307)

Not sure what I've done wrong?

user8620463
  • 43
  • 1
  • 2
  • 8

2 Answers2

2

The expression

(value) and (value+g) not in set(notstorage)

checks if (value) evaluates to True, and that (value+g) not in set(notstorage) evaluates to True.

It does not check if both (value) and (value+g) are not in the set.

Julien
  • 13,986
  • 5
  • 29
  • 53
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

You need to use this syntax instead:

if (value) not in set(notstorage) and (value+g) not in set(notstorage):

Julien
  • 13,986
  • 5
  • 29
  • 53
  • Thank you. I wasn't aware that the syntax I was using wasn't checking the same way for both value and value+g. Much appreciated! – user8620463 Jan 29 '18 at 05:26
  • Question: If I want to add a else operator that returns a specific answer if it can't find any 2 prime numbers separated by a certain number (g) how would I go about doing that, but having it outside of the original loop? I tried adding an else: return('None available') at the very end but that didn't work because then everything just returned that. – user8620463 Jan 29 '18 at 06:08
  • This is unclear and another question altogether, I suggest you create a new one and post all the new relevant code to make it clear – Julien Jan 29 '18 at 06:10
  • Hard to say without seeing your code., but I think all you need is `return values` in the `for` loop when you find them (instead of `break`), and at the end `else: return 'none found'` – Julien Jan 29 '18 at 06:12
  • Ok I'll make a new question. Thanks for your help with this one! – user8620463 Jan 29 '18 at 06:19