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

  • Home
  • SEARCH
  • 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 1066851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:04:31+00:00 2026-05-16T20:04:31+00:00

I wrote this simple code in python to calculate a given number of primes.

  • 0

I wrote this simple code in python to calculate a given number of primes.

The question I want to ask is whether or not it’s possible for me to write a script that calculates how long it will take, in terms of processor cycles, to execute this? If yes then how?

primes = [2]
pstep = 3
count = 1

def ifprime (a):

""" Checking if the passed number is prime or not"""
global primes

for check in primes:
    if (a%check) == 0:
            return False
return True

while 1000000000>= count:

if ifprime(pstep):
    primes.append (pstep)
    print pstep
    count += 1
pstep += 1

The interesting thing about this problem is that whether or not I find primes after x cycles of incrementation is something nearly impossible to predict. Moreover, there’s recursion happening in this scenario since the larger ‘prime’ list grow the longer it will take to execute this function.

Any tips?

  • 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-16T20:04:31+00:00Added an answer on May 16, 2026 at 8:04 pm

    I think you would have to use an approximation of the distribution of primes, a la PNT which (I think) states that between 1 and x you’ll have approximately x/ln(x) primes (ln being natural log). So given rough estimates of the time taken for a single iteration, you should be able to create an estimate.

    You have approximately x/ln(x) primes in your list. Your main code block (inside the while loop) has constant time (effectively)…so:

    t(x) ~ x/ln(x) * a + b + t(x-1)

    where t(x) is the time taken up to and including iteration x, a is the time taken to check each prime in the list (modulous operation), and b is the ‘constant’ time of the main loop. I faintly remember there is a way to convert such recursive functions to linear ones 😉

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

Sidebar

Related Questions

I wrote this simple code (in python) in test.py. I try to run timeit
I wrote this simple C code and compiled it using Visual Studio 2010, with
I've wrote this simple piece of code. And I have a slight problem with
So I wrote this simple console app to aid in my question asking. What
Just for fun, I wrote this simple function to reverse a string in Python:
I wrote this as a simple dice game. It works as I want except
This simple python code: import mmap with file(o:/temp/mmap.test, w+b) as fp: m = mmap.mmap(fp.fileno(),
Python beginner here, so I apologize if this question has a simple answer. (I
I was solving a Python question on CodingBat.com. I wrote following code for a
I am trying to write a simple tool using Shoes. This will indent code

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.