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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:42:10+00:00 2026-05-30T17:42:10+00:00

Here is a program that I wrote in Python: import sys from time import

  • 0

Here is a program that I wrote in Python:

import sys
from time import time
start = time()

x = input('How many primes? ')
if x < 1:
        sys.exit("invalid input")
y = 3
primes = [2]

while len(primes) < x:

        for i in primes:

                if y % i == 0:
                        break
            elif i > (y**(.5)):
                    primes.append(y)
                    break

    y += 1

print primes
print time() - start

It works well, but I have noticed something strange. If I ask for 1000 primes, the program takes about 2.3 seconds to find an answer, but if I ask for 10000 primes, the program takes about 1.8 seconds to find an answer. This doesn’t make much sense because the program has to do more calculations to find 10000 primes than 1000. Can anyone explain what is causing this?

  • 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-30T17:42:11+00:00Added an answer on May 30, 2026 at 5:42 pm

    After fixing one little problem (and removing the output to make things cleaner) it works for me:

    ~/coding$ python pcount.py 
    How many primes? 1000
    0.0194370746613
    ~/coding$ python pcount.py 
    How many primes? 2000
    0.0495121479034
    ~/coding$ python pcount.py 
    How many primes? 5000
    0.172223091125
    ~/coding$ python pcount.py 
    How many primes? 10000 
    0.449481010437
    

    The problem being that your first

    start = time()
    

    line comes before you ask for user input. You’re not timing how long it takes to do the calculations, you’re timing how long it takes you to enter numbers..

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

Sidebar

Related Questions

Here is a special Haskell program which outputs a Python program that outputs a
Here's a piece of code that takes most time in my program, according to
I have a python program which takes input from stdin. Now, I've to write
I wrote a very short program that parses a program using python and converts
Here is a C# program that tries Marshal.SizeOf on a few different types: using
I've got a program that worked until recently. The offending code is shown here:
first question here. I'm developing a program in C# (.NET 3.5) that displays files
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
I am trying to write a cross-platform python program that would run in the
I'm writing a Python program that logs terminal interaction (similar to the script program),

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.