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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:37:49+00:00 2026-05-26T00:37:49+00:00

I am doing a (typical) assignment of finding primes. I thought I’d be clever

  • 0

I am doing a (typical) assignment of finding primes. I thought I’d be clever and, for large numbers, skip the division process with this trick:

def div5(candidate):
    return str(candidate)[-1] == "5"

Adding 5 to itself a few thousand times seems like a waste (I only need the last member), but I wanted to be sure.

credit to unutbu on Measure time elapsed in Python?

%>python -mtimeit -s"import intDiv" "intDiv.div5(2147483645)"
1000000 loops, best of 3: 0.272 usec per loop

%>python -mtimeit -s"import strDiv" "strDiv.str5(2147483645)"
1000000 loops, best of 3: 0.582 usec per loop

For clarification, here are the two methods I defined.

def div5(maxi): return not (maxi%5)

def str5(maxi): return str(maxi)[-1] == '5'

This is too slow. How can I analyze the last member of str(maxi), without converting the whole number (needlessly)?

Thanks to @Claudiu for help with cleaning up the eyesores.

  • 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-26T00:37:50+00:00Added an answer on May 26, 2026 at 12:37 am
    % python -mtimeit "str(2147483645)"
    1000000 loops, best of 3: 0.321 usec per loop
    
    % python -mtimeit "2147483645 % 5"
    10000000 loops, best of 3: 0.0351 usec per loop
    
    % python -mtimeit "'2147483645'[-1]"
    10000000 loops, best of 3: 0.0349 usec per loop
    

    I’d say the bottleneck is converting to a string.

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

Sidebar

Related Questions

I've run into a typical problem here. Till now i was doing strstr($filename,.); $filename
I've been doing web development for a few months now and keep having this
What is the best way of doing version control of large scale refactoring? My
At the moment I'm doing this: gems(id, name, colour, level, effects, source) id is
In javascript, the typical way to new up an object is by doing it
It is typical to have something like this in your cshrc file for setting
I'm doing QA on this new app I've created using CI. With pagination implemented,
This is a typical situation in node.js: asyncFunction(arguments, callback); When asynFunction completes, callback gets
This code compiles but looks very strange. I have a typical and simple parent/child
This is my first experience with EF so I'm probably doing something stupid. Any

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.