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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:46:25+00:00 2026-05-29T15:46:25+00:00

Possible Spoiler Warning I’ve spent ages trying to improve this algorithm and work out

  • 0

Possible Spoiler Warning

I’ve spent ages trying to improve this algorithm and work out what’s going wrong, but I can’t seem to work out why the outputted answer is incorrect.

I’m trying to solve Project Euler #12, to get the lowest triangular number to have over 500 divisors, but it says my answer is incorrect.

Here is my Python code:

import time

# function to get the number of divisors
def div(n):
    d=2
    for i in range(2,int(n**.5)+2):
        if (n % i) == 0:
            d += 1
    return d

start = time.time()

w = True
n=m=1
while w:
    n += 1
    s = (n*(n+1))/2 # nth triangle number
    r = div(s)
    if r > m:
        m = r
        print s,"has",r,"divisors"
        if r > 500:
            w = False

print "Solved in",((time.time()-start)*1000),"milliseconds"

And the output for that code is this (in 66 seconds):

3 has 2 divisors
6 has 4 divisors
36 has 6 divisors
120 has 9 divisors

…

76576500 has 289 divisors
103672800 has 325 divisors
236215980 has 385 divisors
842161320 has 513 divisors
Solved in 65505.5799484 milliseconds

However, if I input 842161320 into the Project Euler problem, it says it’s incorrect.

What am I doing wrong?

  • 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-29T15:46:26+00:00Added an answer on May 29, 2026 at 3:46 pm

    I see two bugs:

    • Your div function is broken: div(24) == 5, while it should be 8
    • Your 1st triangular number would be 3, although it should be 1

    You could implement a working div like this:

    import math
    def divisors(n):
      return sum(1 for x in range(1, n+1) if n % x == 0)
    

    Also, that code is inefficient as hell, some suggestions to improve it are:

    Instead of calculating the nth triangular number using your formula, use a rolling sum:

    import itertools
    
    s = 0
    for i in itertools.count(1):
      s += i
      if div(s) > 500:
        print s
        break
    

    Also, Use prime factors to calculate the number of divisors. You can create a prime factor cache for maximum performance.

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

Sidebar

Related Questions

Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Spoiler alert: this is related to Problem 14 from Project Euler. The following code
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
spoiler : This is just another Lucene vs Sphinx vs whatever, I saw that
This should be easy for many of you, but for me it's just another
Possible Duplicate: how to use foursquare API in android application? This is a question
Possible Duplicate: git - removing a file from source control (but not from the

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.