c math problem reduction
so i came across this question and i was wondering if i could make a program to automate the process, here is the question;
Quote:
Code:
#include <stdio.h> |
The sum of 1+2+3+...+N is N(N+1)/2. (This is easily demonstrated by a recursive argument.) 1000/5=200, so the sum you want is 5*(199*200)/2.
It should be fairly easy for you to write out that answer. . . |
Quote:
Quote:
Edit: PTrenholme gave you an approach that I was planning to only hint at. If you know a little math (or PTrenholme does that part for you) but you aren't very good at arithmetic, you could let the program do the arithmetic as PTrenholme suggested. Alternately, if you had some reason to reject loops and if statements specifically, but not the functional equivalent of loops and if statements, and you also did not want to solve the problem with math as PTrenholme did before writing the program: It is quite simple to use recursion instead of a loop and ?: instead of an if statement. |
probably:
printf("the answer is %20d", 99*1000 + 500); Do you have any (other) algorithm without loop(s), without variables? |
Quote:
|
Quote:
|
Quote:
The folding method (similar to what was shown in the link you provided for Gauss's method) is easier for finding the formula, but may be harder to express with mathematical rigor: To fold the list in half we need an even number of items so insert 0 on the front if we started with an odd length list (as we did this time with 1 through 199). Fold the list so the first number (0 or 1) pairs with the last. If we started with an odd number of numbers then every pair has the same value as 0+N and there are (N+1)/2 pairs so the result is N*(N+1)/2 But if we started with an even number of numbers then there are only N/2 pairs and each has the same value as (1+N) so the result is also N*(N+1)/2. |
Quote:
|
So if the user was required to enter input the program could be similar to ...
Code:
#include <stdio.h> |
Are you aware that steps in a program happen in sequence?
Quote:
I hope you can see why those steps happening in that sequence is incorrect. It is such a basic concept of programming that I don't know how to say it more clearly. I have seen a few recent beginner programmer threads in which that concept was not yet understood, so forgive me if I'm guessing wrong this time and explaining a concept you already knew if you were just careless while constructing the text of the program. |
Quote:
Code:
#include <stdio.h> |
Quote:
Look at each variable that acts as input to any step. Does that variable have the intended value when that step is reached. |
Right i'll reply when i have got the program fixed.
|
BTW, I said your formula was wrong because I misunderstood what you intended N to signify. Then I realized the intent and edited that part out of my post, but you quoted that part before I edited it. Sorry for any confusion that might have caused.
But the formula does need some adjustment if you intend to handle values of N that are not factors of 1000. Think about what happens if N is not a factor of 1000 and what you might do to cover that case (without needing an if statement). |
Here is the edited program seems i used L before declaring it.
Code:
#include <stdio.h> |
All times are GMT -5. The time now is 11:35 AM. |