We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #12: Highly divisible triangular number
Project Euler #12: Highly divisible triangular number
Sort by
recency
|
125 Discussions
|
Please Login in order to post a comment
Pefect Solution In Python You can use the property that the number of divisors of a triangle number T_n = n*(n+1)//2 is the product of the number of divisors of n//2 and n+1 (if n is even), or n and (n+1)//2 (if n is odd). This allows you to factorize smaller numbers and reuse results.
Using Seive C++
include
using namespace std; using ll = long long; ll mx = 1e5; vector cntDiv(mx+1);
void solve () {
}
int main () {
}
Here's an efficient solution in Python that takes advantage of the fact that the formula for the nth triangular number is n * (n+1) // 2 and the fact that n and n + 1 are coprime:
Haskell
include
include
include
using namespace std;
int main() { const int MAX_PRIMES = 1000; vector primes(MAX_PRIMES, 0); primes[0] = 2; primes[1] = 3; primes[2] = 5; primes[3] = 7; int count1 = 4; long long num = 9; while(count1 < MAX_PRIMES) { bool isprime = true; int sqrt_num = sqrt(num);
}