Skip to main content
Code Review

Return to Question

edited tags
Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325
edited title
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

Project Euler 378: Count ordered triplets whose triangular numbers have increasingdecreasing number of factors

kerning
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

Project Euler problem 378 says:

Let \$T(n)\$ be the \$n\$th triangle number, so \$T(n) = {n (n+1) \over 2}\$.

Let \$dT(n)\$\$\mathit{dT}(n)\$ be the number of divisors of \$T(n)\$.
E.g.: \$T(7) = 28\$ and \$dT(7) = 6\$\$\mathit{dT}(7) = 6\$.

Let \$Tr(n)\$\$\mathit{Tr}(n)\$ be the number of triples \$(i, j, k)\$ such that \1ドル ≤ i < j < k ≤ n\$ and \$dT(i) > dT(j) > dT(k)\$\$\mathit{dT}(i) > \mathit{dT}(j) > \mathit{dT}(k)\$.
\$Tr(20) = 14\$\$\mathit{Tr}(20) = 14\$, \$Tr(100) = 5772\$\$\mathit{Tr}(100) = 5772\$ and \$Tr(1000) = 11174776\$\$\mathit{Tr}(1000) = 11174776\$.

Find \$Tr(60 000 000)\$\$\mathit{Tr}(60,000円,000円)\$.
Give the last 18 digits of your answer.

This is my attempt at solving it:

def t(n):
 tri_num = (n * (n+1))//2
 return tri_num
#finding nth triangle numbers
def dt(n):
 count = 0
 for i in range(1,t(n)+1):
 if t(n)%i == 0:
 count += 1
 return count
#finding nth triangle numbers' total number of dividers
def factors(n):
 factors = []
 for i in range(1,t(n)+1):
 if t(n)%i == 0:
 factors.append(i)
 return factors
#finding nth triangle number's dividers
def tr(n):
 triplesnum = 0
 triples = [(i, j, k) for i in range(n) for j in range(n) for k in range(n) if 1 <= i < j < k <= n and dt(i) > dt(j) > dt(k)]
 for i in triples:
 triplesnum += 1
 return triplesnum
while True:
 n = int(input("N =???"))
 print("triples number",tr(n))
#solution

I am new to python; I only know at most the loops,list comprehension, functions, and some class. I am sure this problem can be solved more simply.

Although I see this code as a success considering my knowledge, it works too slowly for me after 3-digit values of n.

Is there an improvement I can make with my current knowledge? If not, what should I be learning about next?

Note: I know some of the code is not necessary, but I used them as a testing material in the further functions.

Project Euler problem 378 says:

Let \$T(n)\$ be the \$n\$th triangle number, so \$T(n) = {n (n+1) \over 2}\$.

Let \$dT(n)\$ be the number of divisors of \$T(n)\$.
E.g.: \$T(7) = 28\$ and \$dT(7) = 6\$.

Let \$Tr(n)\$ be the number of triples \$(i, j, k)\$ such that \1ドル ≤ i < j < k ≤ n\$ and \$dT(i) > dT(j) > dT(k)\$.
\$Tr(20) = 14\$, \$Tr(100) = 5772\$ and \$Tr(1000) = 11174776\$.

Find \$Tr(60 000 000)\$.
Give the last 18 digits of your answer.

This is my attempt at solving it:

def t(n):
 tri_num = (n * (n+1))//2
 return tri_num
#finding nth triangle numbers
def dt(n):
 count = 0
 for i in range(1,t(n)+1):
 if t(n)%i == 0:
 count += 1
 return count
#finding nth triangle numbers' total number of dividers
def factors(n):
 factors = []
 for i in range(1,t(n)+1):
 if t(n)%i == 0:
 factors.append(i)
 return factors
#finding nth triangle number's dividers
def tr(n):
 triplesnum = 0
 triples = [(i, j, k) for i in range(n) for j in range(n) for k in range(n) if 1 <= i < j < k <= n and dt(i) > dt(j) > dt(k)]
 for i in triples:
 triplesnum += 1
 return triplesnum
while True:
 n = int(input("N =???"))
 print("triples number",tr(n))
#solution

I am new to python; I only know at most the loops,list comprehension, functions, and some class. I am sure this problem can be solved more simply.

Although I see this code as a success considering my knowledge, it works too slowly for me after 3-digit values of n.

Is there an improvement I can make with my current knowledge? If not, what should I be learning about next?

Note: I know some of the code is not necessary, but I used them as a testing material in the further functions.

Project Euler problem 378 says:

Let \$T(n)\$ be the \$n\$th triangle number, so \$T(n) = {n (n+1) \over 2}\$.

Let \$\mathit{dT}(n)\$ be the number of divisors of \$T(n)\$.
E.g.: \$T(7) = 28\$ and \$\mathit{dT}(7) = 6\$.

Let \$\mathit{Tr}(n)\$ be the number of triples \$(i, j, k)\$ such that \1ドル ≤ i < j < k ≤ n\$ and \$\mathit{dT}(i) > \mathit{dT}(j) > \mathit{dT}(k)\$.
\$\mathit{Tr}(20) = 14\$, \$\mathit{Tr}(100) = 5772\$ and \$\mathit{Tr}(1000) = 11174776\$.

Find \$\mathit{Tr}(60,000円,000円)\$.
Give the last 18 digits of your answer.

This is my attempt at solving it:

def t(n):
 tri_num = (n * (n+1))//2
 return tri_num
#finding nth triangle numbers
def dt(n):
 count = 0
 for i in range(1,t(n)+1):
 if t(n)%i == 0:
 count += 1
 return count
#finding nth triangle numbers' total number of dividers
def factors(n):
 factors = []
 for i in range(1,t(n)+1):
 if t(n)%i == 0:
 factors.append(i)
 return factors
#finding nth triangle number's dividers
def tr(n):
 triplesnum = 0
 triples = [(i, j, k) for i in range(n) for j in range(n) for k in range(n) if 1 <= i < j < k <= n and dt(i) > dt(j) > dt(k)]
 for i in triples:
 triplesnum += 1
 return triplesnum
while True:
 n = int(input("N =???"))
 print("triples number",tr(n))
#solution

I am new to python; I only know at most the loops,list comprehension, functions, and some class. I am sure this problem can be solved more simply.

Although I see this code as a success considering my knowledge, it works too slowly for me after 3-digit values of n.

Is there an improvement I can make with my current knowledge? If not, what should I be learning about next?

Note: I know some of the code is not necessary, but I used them as a testing material in the further functions.

edited title
Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325
Loading
edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Loading
Edit title to site standard; improved spelling and grammar
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325
Loading
quote the question
Source Link
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 210
Loading
edited tags
Link
Ludisposed
  • 11.8k
  • 2
  • 41
  • 91
Loading
fixed code
Source Link
Loading
added tag
Link
Loading
Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /