Monday, 15 September 2014

python sum of primes -


I'm typing to create a dragon program that will generate the sum of primes for a number but not programmatically Is the correct result, please tell me why.

  b = 1 generates a list of numbers. While B & L; 100: B = B + 1 X = 0.0 A = 0 D = 0 generates a list of numbers less than B. While X & L; B: X = X + 1 # will check for this device if (b / x) -int (b / x) == 0.0: a = a + 1 if a == 2: #if it gets a cardinality So this will add it D = d + b print d   

I have successfully prepared a list of pressure, but I could not find primes to add.

This is the code that I used to generate a list of primes.

  b = 1 while b < 1000: b = b + 1 n = bx = 0.0 a = 0 while x and lt; N: x = x + 1 if (n / x) -int (n / x) == 0.0: a = a + 1 if a == 2: print b    < P> 

Transfer your initialization from that loop to your d variable being reset during each iteration of your external loop .

Additionally, a == 2 check occurs only once the exterior loop is repeated. Take it out of the internal loop.

  b = 1d = 0 # generates a list of numbers. While B & L; 100: B = B + 1 x = 0.0 A = 0 # B produces a list of fewer numbers than X, while X & L; B: X = X + 1 # will check for this device if (b / x) -int (b / x) == 0.0: a = a + 1 if a == 2: #if it gets a cardinality So this will add it to D = D + B print D   

Result:

  1060   

On that, let's try to clean the code so that it is more accessible. You can move the inner loop in your own function so that the reader can understand its purpose more clearly.

  def is_prime (b): x = 0.0 a = 0 while x & lt; B: x = x + 1 will check for divisors if (b / x) -int (b / x) == 0.0: a = a + 1 if a == 2: returns true else: return false b = 1 d = 0 # generates a list of numbers. While B & L; 100: B = B + 1 if is_prime (B): D = D + B print D   

They are also useful for using variable names that represent them:

  def is_prime (number): candidate_factor = 0 amount_off_actor = 0 while candidate_actor & lt; Number: # A + = A is equal to A = A candidate_factor == 1 # What is an easy way to test the number one more evenly if the number% candidate_factor == 0: zodiac_off_actor == 1 if sum_off_actors == 2 : Returns True else: return False number = 1 prime_total = 0 # raises a list of numbers. While the number & lt; 100: number + = 1 if_prime (number): prime_total + = number print prime_total    for loops  / code> loop which increases a counter The range for the candidate_factor (1, number + 1): if number% candidate_factor == 0: amount_of_factors + = 1 if zodiac_of_actor = = 2: Return is true: Return wrong prime_total = 0 # gives rise to a list of numbers. For the number in the range (2, 101): If is_prime (number): prime_total + = number print prime_total   

If you are feeling bold, you can cut down on the list The number of loops that are using numbers can be used for the number of loops using the numbers:

  def is_prime (number): factor = [candidate for candidate / factor category (1, number + 1) Number if% candidate_factor == 0] back lane (factor) == 2 # gives rise to a list of numbers Primes = [number of numbers Number of Kya (2, 101) if Is_prime (number)] prime_total = sum (primes) Print prime_total    

No comments:

Post a Comment