Home > Mobile >  Creating a python loop/ list comprehension that contains all the multiples of 1 to 100 that are unde
Creating a python loop/ list comprehension that contains all the multiples of 1 to 100 that are unde

Time:10-09

I want to create a python list comprehension statement that contains all the multiples of 2 to 100 that are under 100 and puts them inside a list. I understand this statement is probably very confusing so here how it would work in my mind:

  1. Start from 2, add all the multiples of 2 to a list. IE. 2, 4, 6, 8... 100..
  2. Move onto the next number, 3, add all the multiple of 3 to the same list(keeping it under 100) IE. 3, 6, 9, …99
  3. Repeat this for all number from 2 to 99, placing all the values inside one singular list
  4. In the end, my list should pretty much contain every number from 1 to 100 that is not prime.

I want to achieve this using list comprehension if possible. I've attached my work in the bottom which is not working. Right now, my loop just prints the multiple of 2 100 times and I am having trouble figuring out how to change the number being multiplied and storing it in one entire list.

Note: The point of my list is to filter out the prime numbers later, in the fashion of the Sieve of Eratosthenes but I don't need help with that part. See my code below:

print([i for i  in range(100) if [x x for x in range(100) if i   i <= 100]])

CodePudding user response:

In my early days with Python I often found it helpful to write the operation as a regular loop first, and then converting it to a list comprehension

I'm going to use a set comprehension in my answer because sets get rid of the duplicates, so you don't have to worry about checking if a number is already in the list.

For example:

multiples = set()

for i in range(2, 101): # Loop over numbers
    for j in range(2, 100//i   1): # Loop over multiplications
        multiples.add(i * j)

The inner loop goes from 2 (because you don't want to add every number to the set) to 100//ii 1 (because 100//i is the highest multiple of i that is <= 100, and 1 because range() excludes the end).

Writing this as a listset comprehension now is easy:

multiples = {i * j for i in range(2, 101) for j in range(2, 100//i   1)}

To quickly pick out the numbers that aren't in this set, you can create a set containing all numbers from 2 (because 1 isn't prime) to 100 and get the set difference.

primes = set(range(2, 101)) - multiples

which gives:

{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}

CodePudding user response:

This is not an answer, just an observation about how list comprehension could be slower than a multiline program.

This program is an implementation of the Sieve of Eratosthenes. One line added to @Pranav's solution could eliminate running through the table with unnecessary divisors. e.g.

multiples = set()

for i in range(2, 101): # Loop over numbers
    if not i in multiples:
       for j in range(2, 100//i   1): # Loop over multiplications
           multiples.add(i * j)

I don't believe that could be done with any kind of list comprehension.

  • Related