0
def xPrimes(x) :
  y = 2
  while y < x :
      if isItPrime(y) == True :
        y += 1
      y += 1
  print(primes)

I am a beginner in python and I'm having trouble having the program do what's needed. I also don't fully understand what my program does. When I did xPrimes(5), it gave me [2,3,5] instead of [2,3,5,7,11]. My code prints all prime numbers UP to x instead of x prime numbers. I suspect that I need a counter but I don't know where to implement it.

BriK
  • 19
  • 4
  • 4
    Do you know what a prime number is? Would you know how to find the first 10 primes using just paper and pencil? You should make the function do the same thing you would do. – zvone Nov 16 '19 at 17:41
  • I have already implemented isItPrime to find and add all primes to the list of primes. – BriK Nov 16 '19 at 17:45
  • 1
    Possible duplicate of [Print series of prime numbers in python](https://stackoverflow.com/questions/11619942/print-series-of-prime-numbers-in-python) – Joseph Wood Nov 16 '19 at 17:51

3 Answers3

2

You just need to keep generating primes until you have x of them. If you're returning a list of the results, your counter is the length of that list.

def xPrimes(x: int) -> List[int]:
    primes: List[int] = []
    y = 1
    while(len(primes) < x):
        y += 1
        if isItPrime(y):
            primes.append(y)
    return primes

Note that y is the prime number and x is the number of prime numbers and that these are completely different numbers. :)

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • 1
    Yeah, note @BriK that your `xPrimes` took a number of primes `x` as an argument, but when you compared `y` to `x`, you were comparing the next prime _candidate_, not the number of primes already generated. – erik258 Nov 16 '19 at 17:45
0

What is primes here ?

you have to treat x as counter and do not compare with y as y is prime number not a counter

instead you can do something like this

def xPrimes(x) :
  y = 2
  index = 0
  while index < x :
      if isItPrime(y) == True :
        y += 1
        index+= 1
      y += 1
  print(primes)

you can see, I have use index as counter and incremented it when I got prime number

Sagan Pariyar
  • 200
  • 1
  • 1
  • 11
0

Homework is an adventure and chance to experiment!

First, you asked why xPrimes(5) gave you up to 5. You stop your loop when y < x, and y goes up each time. This shows you that you can get lost by using small variable names. You could make your code look like this by just renaming things:

def primes_up_to_number(stop_at) :
  testing_number = 2
  while testing_number < stop_at :
      if isItPrime(testing_number) == True :
        testing_number += 1
      testing_number += 1
  print(primes)

This is confusing to me, as you can't get the output [2, 3, 5]. When testing_number is 2, you add one to it, then add one again before you check isItPrime again, checking 4. I am assuming isItPrime updates some global array primes.

I think you want to change the code and meaning from stop_at to number_of_primes. If so, you should set a counter number_of_primes_found = 0 at start of the function and add one to it each time you find a prime. You should change the expression in the while loop to keep looping until that number of primes is found.

Have a great day! Keep coding! Keep notes.

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83