Now what is happening is, for every instance of a prime number x (the prime number) is being added (appended) to the list primeNums. Again, using the if statement from before: #defining the list The method will be complete at this point and will return primelist. If num is prime then it will be appended to the list, primelist. Once the second for loop is iterated through, the first for loop will ask if num is prime. If num modulo i is 0 then prime is False. To create a list of prime numbers, you must first define a list, and then add the prime numbers to that list. Another for loop is created where i iterates through the numbers 2 and num. What this does now, is for every instance of a prime number, you will add 1 to the count of prime numbers. Thus what is happening, when you use 100 as your num your function will return 1060, as this is the sum of all the primes up to 100.įirst, if you want to know the total amount of prime numbers up to a certain number, you can use the following: #define a variable to track the total number of primes ![]() ![]() The problem lies within this: if sympy.isprime(x) = True:Ībove, when you are doing primes = primes + x what you are saying is that you want to add the value of the x (a prime number) to the variable primes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |