|
|
You may use library functions that you know of, (or mathematical formulas) to make your program run faster. |
"Given n, solve for m" |
n * n = m*(m+1)/2 |
n
is known (it is the for loop variable).m
as the unknown.
|
|
m
corresponding to the current value of n
. Then verify that this is an integer solution, if so print out m and n.double
). But when verifying the result, I too used the 64-bit integer type unsigned long long
, in order to avoid approximations and loss of precision which can occur with floating-point calculations. Hence you may need different types of variables at different stages.
|
|
sqrt
returns a double
, so you would need to cast that back to an integral type before comparing it to m
unsigned long long int
, I find it easier to use std::size_t
|
|
You don't need the inner loop at all. Given n, solve for m and then see if it's an integer. |
long double
here as it gives slightly better precision than double.m
and n
of type long double.
|
|
|
|
|
|
double
, but because of a lack of precision it gave both the correct results, as well as a lot of incorrect result. I think the extra precision of long double should be sufficient here - otherwise you may need to verify the result another way. I've said more than enough for one post, so I'll leave it there for now.
|
|
36 == 6*6 == 1 + ... + 8 1225 == 35*35 == 1 + ... + 49 41616 == 204*204 == 1 + ... + 288 1413721 == 1189*1189 == 1 + ... + 1681 48024900 == 6930*6930 == 1 + ... + 9800 1631432881 == 40391*40391 == 1 + ... + 57121 55420693056 == 235416*235416 == 1 + ... + 332928 1882672131025 == 1372105*1372105 == 1 + ... + 1940449 63955431761796 == 7997214*7997214 == 1 + ... + 11309768 2172602007770041 == 46611179*46611179 == 1 + ... + 65918161 73804512832419600 == 271669860*271669860 == 1 + ... + 384199200 |
|
|
1 == square of 1 == sum of positive integers up to 1 36 == square of 6 == sum of positive integers up to 8 1225 == square of 35 == sum of positive integers up to 49 41616 == square of 204 == sum of positive integers up to 288 1413721 == square of 1189 == sum of positive integers up to 1681 48024900 == square of 6930 == sum of positive integers up to 9800 1631432881 == square of 40391 == sum of positive integers up to 57121 55420693056 == square of 235416 == sum of positive integers up to 332928 1882672131025 == square of 1372105 == sum of positive integers up to 1940449 63955431761796 == square of 7997214 == sum of positive integers up to 11309768 2172602007770041 == square of 46611179 == sum of positive integers up to 65918161 73804512832419600 == square of 271669860 == sum of positive integers up to 384199200 2507180834294496361 == square of 1583407981 == sum of positive integers up to 2239277041 |
|
|
square of 1 == sum of integers up to 1 square of 6 == sum of integers up to 8 square of 35 == sum of integers up to 49 square of 204 == sum of integers up to 288 square of 1189 == sum of integers up to 1681 square of 6930 == sum of integers up to 9800 square of 40391 == sum of integers up to 57121 square of 235416 == sum of integers up to 332928 square of 1372105 == sum of integers up to 1940449 square of 7997214 == sum of integers up to 11309768 square of 46611179 == sum of integers up to 65918161 square of 271669860 == sum of integers up to 384199200 square of 1583407981 == sum of integers up to 2239277041 square of 9228778026 == sum of integers up to 13051463048 square of 53789260175 == sum of integers up to 76069501249 square of 313506783024 == sum of integers up to 443365544448 square of 1827251437969 == sum of integers up to 2584123765441 square of 10650001844790 == sum of integers up to 15061377048200 square of 62072759630771 == sum of integers up to 87784138523761 square of 361786555939836 == sum of integers up to 511643454094368 square of 2108646576008245 == sum of integers up to 2982076586042449 square of 12290092900109634 == sum of integers up to 17380816062160328 square of 71631910824649559 == sum of integers up to 101302819786919521 square of 417501372047787720 == sum of integers up to 590436102659356800 square of 2433376321462076761 == sum of integers up to 3441313796169221281 |
|
|