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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:38:09+00:00 2026-06-17T20:38:09+00:00

I wrote a program in Python to generate prime numbers here is the program

  • 0

I wrote a program in Python to generate prime numbers
here is the program

def genPrimes(n):
    primes = [2]  # primes generated so far
    last = 3  # last number tried
    while last <= n:        
        for p in primes:
            if last % p == 0 and math.sqrt(p) <= last:
                break
        else:
            primes.append(last)
        last += 2
    return primes

http://codepad.org/d33tsQyT

This program is producing a right answer. If you see the indentation for else: statement it is wrongly placed. if i try to place the else statement in if block interpreter is showing memory error. Can anyone tell why this is happening.

Thanks in advance
Maries

  • 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-17T20:38:10+00:00Added an answer on June 17, 2026 at 8:38 pm

    The else is actually attached to the for loop, and executes if the program doesn’t break out of the loop. In your case, it executes if none of the primes divide into the number, so the number is prime and gets appended to the list.

    See also the documentation.

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

Sidebar

Related Questions

I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The
A while back I wrote a simple python program to brute-force the single solution
I wrote a basic Hippity Hop program in C, Python, and OCaml. Granted, this
I wrote a program in python (using standard python libraries) long ago. Now I
I wrote a CPU monitoring program in Python. For some reason sometimes the the
I wrote a very short program that parses a program using python and converts
I wrote a Python program that acts on a large input file to create
I wrote a python program that needs to call aircrack program to some tasks,
Inspired by the top answer on this page I wrote a python program to
I have a python program I wrote that I am trying to compile with

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.