python program for finding triangular number with more than 500 divisors

Question:

I have tried to solve a problem to find first triangular number with more than 500 divisors but there is overflow error

please give a better way

question is

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66 …

Let us list the factors of the first seven triangle numbers:

  • 1: 1
  • 3: 1,3
  • 6: 1,2,3,6
  • 10: 1,2,5,10
  • 15: 1,3,5,15
  • 21: 1,3,7,21
  • 28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?

my program is

def isPrime(a):
    m=0
    for j in range(1,a/2+1):
        if (a%j)==0:
            m+=1
    return m+1
i=1
n=1
div=500
while (i>=1):
    l=isPrime(i)
    ans=i
    if l>div:
        print ans
        break
    n+=1
    i=n*(n+1)/2
Asked By: jainaman224

||

Answers:

The key to this problem is how to make your program more efficient, you have to write something that calculates the number of divisors of an integer in a more efficient way. There are plenty of discussions on how to make this in this forum, like here: What is the best way to get all the divisors of a number?
However, I still think your program has some problems, so I wrote one(still using your method of finding the number of divisors):

def numberOfDivisors(a):
    m=0
    for j in range(1,a/2+1):
        if (a%j)==0:
            m+=1
    return m+1

def findNumber():
    n = 1
    i = 1
    div = numberOfDivisors(n)
    while div < 500:
        i += 1        
        n = i*(i+1)/2
        div = numberOfDivisors(n)
        print 'n = ', n
    return n

The unnecessary print is there just to make it easier to follow the way the code is working. As I said, still using the very poor method of finding the number of divisors. Making it efficient is the way to solve the question. This program will not work as you want to, because it will be too slow when it reaches 6 or 7-digit numbers.

One simple but more efficient way to find the number of divisors is to use the prime factorization of the number to generate the number of divisors. Finding the prime factorization is well documented, but I would suggest using a “Sieve of Eratosthenes” or something of the like (link to wiki)

Once you have the prime factorization, you just need to find the number of permutations that exist from 0 to the power of each prime in the factorization as described here. No need to actually find all of the factors 🙂

That is how I just found the solution to this same problem, and my program was able to test the first 12,375 triangular numbers in just under 9 seconds.

Edit: I should mention that you probably need to look into recursive functions if you want to pursue this method, as you’ll need it to find all of the permutations of the powers found in the prime factorization

Answered By: mscottchristensen
num = 1


# Find the value of the nth triangular number:
def triangularNumber(nth):
    return nth * (nth + 1) // 2


# Find the number of divisors for a given triangular number:
def numberOfDivisors(someNumber):
    divisorCount = 0
    i = 1
    while i * i <= someNumber:
        if someNumber % i == 0:
            divisorCount += 1
            if i * i != someNumber:
                divisorCount += 1
        i += 1
    return divisorCount


# Find the first triangular number to have over 500 divisors:
while True:
    triNum = (triangularNumber(num))
    divisors = numberOfDivisors(triNum)
    if divisors > 500:
        print(triNum)
        break
    num += 1

It runs in less than 2 seconds for 500 divisors.
Thanks!

Answered By: Sigmatest
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.