Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Python Challenges: Find the first triangle number to have over n divisors

Python Challenges - 1: Exercise-41 with Solution

Write a Python program to find the first triangle number to have over n(given) divisors.

From Wikipedia: A triangular number is a number that is the sum of all of the natural numbers up to a certain number. For example, 10 is a triangular number because 1 + 2 + 3 + 4 = 10. The first 25 triangular numbers are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, and 351.
A triangular number is calculated by the equation: n(n+1)/2
The factors of the first five triangle numbers:
1: 1
3: 1, 3
6: 1, 2, 3, 6
10: 1, 2, 5, 10
15: 1, 3, 5, 15
In the above list 6 is the first triangle number to have over four divisors.

Sample Solution:

Python Code:

from functools import reduce
def divisors(n):
    expList = []
    ctr = 0
    divisor = 2
    while divisor <= n:
        while n % divisor == 0:
            n = n/divisor
            ctr += 1
        if ctr != 0:
            expList.append(ctr+1)
        divisor += 1
        ctr = 0
    return reduce(lambda n, y: n * y, expList, 1)


def n_divisors(n):
    natural = 1
    triangular_num = 0

    while True:
        triangular_num += natural
        natural += 1
        if divisors(triangular_num) > n:
            break
    return triangular_num

print(n_divisors(5))
print(n_divisors(100))

Sample Output:

28
73920

Flowchart:

Python Flowchart: Find the first triangle number to have over n divisors.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to find the product xyz.

Next: Write a Python program to find the starting number, under ten thousand will create the longest chain.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m