A triangular number is a product of three factors as follows:
$$ \text{Triangular number} = x(x + 1)(x + 2) $$
Is there a way to make this code faster? As it is the code calculates every triangular number less than or equal to the integer given by the user.
#include <stdio.h>
int main(void) {
int firstFactor = 0;
int secondFactor = 1;
int thirdFactor = 2;
int userInput;
int product = 0;
printf("Enter a integer: ");
scanf("%d", &userInput);
if(userInput == 0) {
printf("User input is a triangular number\n");
return 0;
}
do {
firstFactor++;
secondFactor++;
thirdFactor++;
product = firstFactor * secondFactor * thirdFactor;
} while(product < userInput);
if(product == userInput) {
printf("User input is a triangular number\n");
} else {
printf("User input is not a triangular number\n");
}
return 0;
}
int n
everything is \$O(1)\$). \$O(\log \log n)\$ seems more likely. \$\endgroup\$nth triangular number
but6 * nth triangular number
. That's right ? triangular number \$\endgroup\$