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 8900473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:06:23+00:00 2026-06-15T01:06:23+00:00

def all_primes(start,end): list_nonprimes = [] list_primes = [] for i in range(start,end): for a

  • 0
def all_primes(start,end):
    list_nonprimes = []
    list_primes = []

    for i in range(start,end):
        for a in range(2,i):
            if i % a == 1 and i not in list_nonprimes:
                if i not in list_primes:
                    list_primes.append(i)
            else:
                list_nonprimes.append(i)

    return list_primes

Why is this giving me an an incorrect output?

>>> all_primes(1,10)
[3,5,7,9]

How do I eliminate the 9?

  • 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-06-15T01:06:24+00:00Added an answer on June 15, 2026 at 1:06 am

    There’s a more straightforward way to do this, since you’re already inherently generating the list of primes less than the number you’re currently checking:

    def all_primes(start,end):
        list_primes = []
    
        for i in range(2,end):
            for a in list_primes:
                if i % a == 0:
                    break
            else:
                list_primes.append(i)
    
        return [x for x in list_primes if x >= start]
    

    Key to understanding this is knowing how the for...else construct works in Python. Essentially, a for loop can have an else statement, which is only executed if no break statement was run during the evaluation of the loop.

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

Sidebar

Related Questions

def primes(n): if n==2: return [2] elif n<2: return [] s=range(3,n+1,2) mroot = n
This is the best algorithm I could come up. def get_primes(n): numbers = set(range(n,
I have this simple code in my user_controller.rb file #listing all users def index
I have the following code: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing
I have methods in all of my models that look like this: def formatted_start_date
I want to sum columns. In my controller def index @performance_reports = PerformanceReport.all end
i have this class PagesController < ApplicationController def index @textos = Texto.all @texto_historia =
I've been trying to get this code to work to return a input number
In Wikipedia this is one of the given algorithms to generate prime numbers: def
class A(models.Model): def calculate(self): # calculate some field based on all self.b_set instances class

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.