0

I'm getting back into Python after some time away in Javascript. I'm currently trying to write a function that tests a set of numbers to see if they're prime.

I know how to do a simple function which does this, but I'm currently having some trouble understanding how to implement it into the test. Any help would be much appreciated.

def first_100_prime_numbers():
    for prime in primes:
        if (prime > 2) & (prime % 2 != 0):
            return primes

def test():
    primes = set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541])

    if set(first_100_prime_numbers()) == primes:
        print("Correct.")
    else:
        print("Answer is wrong. Keep trying!")

test()
Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
Kevin Hebert
  • 63
  • 1
  • 7

2 Answers2

0

Well in this line you need to give some argument

def first_100_prime_numbers():

needs to be like

def first_100_prime_numbers(primes):

and don't forget to actually pass it something when you call it

if set(first_100_prime_numbers(primes)) == primes:
SuperStew
  • 2,857
  • 2
  • 15
  • 27
  • Hmm I thought that was the solution but I keep getting that 'primes' is not defined' when i try to pass it in as an argument @SuperStew – Kevin Hebert Sep 24 '18 at 15:01
0

First things first

a & b executes the bitwise operator "and" between a and b. You are sure that you want to use the bitwise operator there? To apply the logical operator "and" between a and b you need to use a and b. Take a look on this reference.

Check the identation

I tried to run your code with the corrections proposed by @SuperStew and it worked as expected, printing "Correct." on the console. I also noted that your code identation are not right and edited it. Maybe that was the problem.

Last but not least

You know that this function of yours do not accomplish what you propose, right? You should check if the number is not divisible by all previous prime numbers too. That are other errors too but as far as I get that is not the point of the question.

Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
  • Thanks so much mate. The bitwise operator and the indentation were definitely throwing things off (which I couldn't really tell with the in-browser IDE I'm using). LOL it's also quite an early morning over here and I'm just now realizing that you're right; my function does not accomplish my goal. I guess I should've looked over it more myself before posting. – Kevin Hebert Sep 24 '18 at 15:37
  • You're welcome, an answer acceptance would be appreciated ;) – Hemerson Tacon Sep 24 '18 at 15:42
  • 1
    You got it mate :D (That's the checkmark button, right?) – Kevin Hebert Sep 24 '18 at 15:43