Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6130743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:53:38+00:00 2026-05-23T16:53:38+00:00

This is my code for finding primes using the Sieve of Eratosthenes. list =

  • 0

This is my code for finding primes using the Sieve of Eratosthenes.

list = [i for i in range(2, int(raw_input("Compute primes up to what number? "))+1)]  

for i in list:
    for a in list:
            if a!=i and a%i == 0:
                list.remove(a)

Trying to find a way to compress those nested for loops into some kind of generator or comprehension, but it doesn’t seem that you can apply a function to a list using a comprehension. I tried using map and filter, but I can’t seem to get it right.

Thinking about something like this:

print map(list.remove(a), filter(lambda a, i: (a%i ==0 and a!=i), [(a, i) for i in list for a in list])

Obviously doesn’t work for a dozen reasons. If I just was using the filter portion of that code:

filter(lambda a, i: (a%i ==0 and a!=i), **[(a, i) for i in list for a in list]**

What’s the proper way of putting two variables into the lambda? (a,i) makes it a tuple, but I want to submit ‘a’ and ‘i’ as independent variables to put into the lambda.

I ended up resolving the problem with this one-liner:

print sorted(set([i for i in range(2, int(raw_input("Compute primes up to what number? "))+1)]).difference(a for i in l for a in l if a!=i and a%i == 0))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:53:39+00:00Added an answer on May 23, 2026 at 4:53 pm

    It’s not precisely a direct translation of your loops, but it’s quite close and compact:

    >>> l = range(2, 101)
    >>> sorted(set(l).difference(a for i in l for a in l if a!=i and a%i == 0))
    [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]
    

    Although I’d suggest a > i rather than a != 0 as being shorter and faster 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen this code for finding a Minor of a matrix: RegMatrix RegMatrix::Minor(const int
What is wrong with this code. The code is finding the javascript and debug1
I have trouble finding way to correctly refactor this code so that there would
I am finding I'm writing a lot of code that looks like this: <div
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
I am using this code... Reachability *r = [Reachability reachabilityWithHostName:@www.maxqdata.com]; NetworkStatus internetStatus = [r
I am using ASP.NET to transmit a .jar file. This code works perfectly on
Why does this code return the sum of factors of a number? In several
This is my code for finding the largest word from a given string. I
I use this code for finding the debug directory public string str_directory = Environment.CurrentDirectory.ToString();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.