Skip to main content
added 43 characters in body
Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70

My code is a general solution to the following problem from Project EulerProject Euler #12:

What is the value of the first triangle number to have over five hundred divisors?

MY CODE

#include <iostream>
int generate_triangular(int n);
int enumerate_divisors(int n);
int main()
{
    std::cout<<"You want to find the first triangular number with less than or more than how many divisors? "<<std::endl;
    int user_input;
    std::cin>>user_input;
    bool result_found = false;
    int test_number = 1;
    int target;
    while(!result_found){
        int factor_count = enumerate_divisors(generate_triangular(test_number));
        if(factor_count < user_input){
            test_number++;
        }else{
            result_found = true;
            target = generate_triangular(test_number);
        }
    }
    std::cout<<"The "<<test_number<<" th triangular number"<<std::endl;
    std::cout<<target<<std::endl;
}
int generate_triangular(int n){
    return (n*(n+1))/2;
}
int enumerate_divisors(int n){
    int divisor_count = 0;
    for(int i = 1;i*i <= n;i++){
        if(n%i == 0){
            divisor_count++;
        }
    }
    return 2*divisor_count;
}

I found my solution to be quite fast for the purposes of the question (it found the first triangular number with over 500 divisors in about .2 seconds). Wondering how I can make it faster, and about any general code suggestions to be made.

My code is a general solution to the following problem from Project Euler:

What is the value of the first triangle number to have over five hundred divisors?

MY CODE

#include <iostream>
int generate_triangular(int n);
int enumerate_divisors(int n);
int main()
{
    std::cout<<"You want to find the first triangular number with less than or more than how many divisors? "<<std::endl;
    int user_input;
    std::cin>>user_input;
    bool result_found = false;
    int test_number = 1;
    int target;
    while(!result_found){
        int factor_count = enumerate_divisors(generate_triangular(test_number));
        if(factor_count < user_input){
            test_number++;
        }else{
            result_found = true;
            target = generate_triangular(test_number);
        }
    }
    std::cout<<"The "<<test_number<<" th triangular number"<<std::endl;
    std::cout<<target<<std::endl;
}
int generate_triangular(int n){
    return (n*(n+1))/2;
}
int enumerate_divisors(int n){
    int divisor_count = 0;
    for(int i = 1;i*i <= n;i++){
        if(n%i == 0){
            divisor_count++;
        }
    }
    return 2*divisor_count;
}

I found my solution to be quite fast for the purposes of the question (it found the first triangular number with over 500 divisors in about .2 seconds). Wondering how I can make it faster, and about any general code suggestions to be made.

My code is a general solution to the following problem from Project Euler #12:

What is the value of the first triangle number to have over five hundred divisors?

MY CODE

#include <iostream>
int generate_triangular(int n);
int enumerate_divisors(int n);
int main()
{
    std::cout<<"You want to find the first triangular number with less than or more than how many divisors? "<<std::endl;
    int user_input;
    std::cin>>user_input;
    bool result_found = false;
    int test_number = 1;
    int target;
    while(!result_found){
        int factor_count = enumerate_divisors(generate_triangular(test_number));
        if(factor_count < user_input){
            test_number++;
        }else{
            result_found = true;
            target = generate_triangular(test_number);
        }
    }
    std::cout<<"The "<<test_number<<" th triangular number"<<std::endl;
    std::cout<<target<<std::endl;
}
int generate_triangular(int n){
    return (n*(n+1))/2;
}
int enumerate_divisors(int n){
    int divisor_count = 0;
    for(int i = 1;i*i <= n;i++){
        if(n%i == 0){
            divisor_count++;
        }
    }
    return 2*divisor_count;
}

I found my solution to be quite fast for the purposes of the question (it found the first triangular number with over 500 divisors in about .2 seconds). Wondering how I can make it faster, and about any general code suggestions to be made.

Source Link
Dunka
  • 143
  • 3

Find The Triangular Number With More Than N Divisors

My code is a general solution to the following problem from Project Euler:

What is the value of the first triangle number to have over five hundred divisors?

MY CODE

#include <iostream>
int generate_triangular(int n);
int enumerate_divisors(int n);
int main()
{
    std::cout<<"You want to find the first triangular number with less than or more than how many divisors? "<<std::endl;
    int user_input;
    std::cin>>user_input;
    bool result_found = false;
    int test_number = 1;
    int target;
    while(!result_found){
        int factor_count = enumerate_divisors(generate_triangular(test_number));
        if(factor_count < user_input){
            test_number++;
        }else{
            result_found = true;
            target = generate_triangular(test_number);
        }
    }
    std::cout<<"The "<<test_number<<" th triangular number"<<std::endl;
    std::cout<<target<<std::endl;
}
int generate_triangular(int n){
    return (n*(n+1))/2;
}
int enumerate_divisors(int n){
    int divisor_count = 0;
    for(int i = 1;i*i <= n;i++){
        if(n%i == 0){
            divisor_count++;
        }
    }
    return 2*divisor_count;
}

I found my solution to be quite fast for the purposes of the question (it found the first triangular number with over 500 divisors in about .2 seconds). Wondering how I can make it faster, and about any general code suggestions to be made.