ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

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:

Find the sum of all the multiples of 5 below 1000?

Is it possible to make a program to automate the process without using loops or if statements? And also without declaring a variable that stores the value of all the multiplies of 5 under 1000 and the calling printf to output the result?..like..so

It is hard to guess what you mean by "automate" in that context.

Quote:

Is it possible to make a program to automate the process without using loops or if statements?

You could easily write a program that displays the correct result and does not need any loops or if statements. Simply compute the answer in your head (anyone with a strong grasp of high school algebra could do so) and Write a program that simply displays that number.

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.

I think a recursive method is easier for constructing a mathematically rigorous proof that the formula is correct after you have already found the formula.

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.

So if the user was required to enter input the program could be similar to ...

Code:

#include <stdio.h>
int main(void)
{
int N, L, M;
L = 1000/N;
M = L - 1;
printf("Enter a number below 1000: ");
scanf("%d", &N);
printf("By recursive argument: %3d\n", L);
printf("So the sum of the program is: %10d", N*(M*L)/2);
return 0;
}

Are you aware that steps in a program happen in sequence?

Quote:

Originally Posted by curious95

Code:

int N, L, M;
L = 1000/N;
M = L - 1;
printf("Enter a number below 1000: ");
scanf("%d", &N);

The line I marked in red happens before the line I marked in purple.

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.

Are you aware that steps in a program happen in sequence?

The line I marked in red happens before the line I marked in purple.

Your formula is wrong anyway.

But the basic concept that these steps happen in sequence is even more important.

Code:

#include <stdio.h>
int main(void)
{
int N, L, M;
M = L - 1;
printf("Enter a number below 1000: ");
scanf("%d", &N);
printf("By recursive argument: %3d\n", L);
L = 1000/N;
printf("So the sum of the program is: %10d", N*(M*L)/2);
return 0;
}

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>
int main(void)
{
int N, L, M;
printf("Enter a number below 1000: ");
scanf("%d", &N);
L = 1000/N;
M = L - 1;
printf("By recursive argument: %3d\nSo the sum of the program is: %5d\n", L, N*(M*L)/2);
return 0;
}

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.