Python Forum
Divide a number by numbers in a list.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Divide a number by numbers in a list.
#4
No, you should change

for i in range(1, int(test**0.5)+1, int(primes)):
to

for prime in primes:
You can loop directly through the items in a list this way. So the first time through the loop it will be 2, the second time it will be 3, and the 5, and so on.

But you will have other problems. After the test for each prime, you are adding the prime. Say you get to 9. You will check 9 against 2, and find it doesn't divide evenly. Then you will save 9 as a prime and add it to sum, but 9 is not prime.

You need to check that all of the primes do not divide evenly in the number you are checking. I would typically do it this way:

for counter in range(3, 10000, 2):
    for prime in primes:
        if counter % prime == 0:
            break
    else:
        primes.append(counter)
total = sum(primes)
The first loop goes through the odd numbers (using step = 2) from 3 to 10000. The second loop goes through any primes you've found. The key is the else statement after the inner for loop. Else statements after loops trigger if the loop exits without a break statement. In this case, the break statement happens if a prime factor is found. So the else statement only triggers if no prime factors are found. Then at the end, you use the sum function to total the primes, rather than keeping a running total.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Divide a number by numbers in a list. - by Wallen - Sep-14-2018, 01:46 PM
RE: Divide a number by numbers in a list. - by ichabod801 - Sep-14-2018, 05:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 16,109 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Delete strings from a list to create a new only number list Dvdscot 8 1,780 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,692 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 3,207 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,541 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,693 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,099 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,554 Apr-28-2022, 09:32 AM
Last Post: Dexty
  When did the number got included in the list? Frankduc 14 3,378 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  producing numbers out of a list bouraque7878 10 3,943 Nov-12-2021, 09:13 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020