Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] c math problem reduction
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

12-12-2012, 01:10 PM   #1
curious95
Member

Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83

Rep:
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:
 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
Code:
```#include <stdio.h>

int main(void)
{
int mult;
mult = 5 + 10 + 15 + 20 + 25 + 30 + 35 + 40 + 45 + 50 + 55 + 60 + 65 + 70 + 75 + 80 + 85 + 90 + 95 + 100 + 105 + 110 + 115 + 120 + 125 + 130 + 135 + 140 + 1
45 + 150 + 155 + 160 + 165 + 170 + 175 + 180 + 185 + 190 + 195 + 200 + 205 + 210 + 215 + 220 + 225 + 230 + 235 + 240 + 245 + 250 + 255 + 260 + 265 + 270 + 275
+ 280 + 285 + 290 + 295 + 300 + 305 + 310 + 315 + 320 + 325 + 330 + 335 + 340 + 345 + 350 + 355 + 360 + 365 + 370 + 375 + 380 + 385 ...;
//and so on until 995
return 0;
}```

Last edited by curious95; 12-12-2012 at 01:11 PM.

 12-12-2012, 01:28 PM #2 PTrenholme Senior Member Contributing Member   Registered: Dec 2004 Location: Olympia, WA, USA Distribution: Fedora, (K)Ubuntu Posts: 4,154 Rep: 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. . .
12-12-2012, 01:30 PM   #3
johnsfine
Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,142

Rep:
Quote:
 Originally Posted by curious95 make a program to automate the process,
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.

Last edited by johnsfine; 12-12-2012 at 01:39 PM.

 12-12-2012, 01:38 PM #4 pan64 Guru   Registered: Mar 2012 Location: Hungary Distribution: debian i686 (solaris) Posts: 5,159 Rep: probably: printf("the answer is %20d", 99*1000 + 500); Do you have any (other) algorithm without loop(s), without variables? Last edited by pan64; 12-12-2012 at 02:06 PM.
12-12-2012, 01:39 PM   #5
linosaurusroot
Member

Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 870
Blog Entries: 2

Rep:
Quote:
 Originally Posted by PTrenholme easily demonstrated by a recursive argument
I think Gauss's method is simpler. http://www.jimloy.com/algebra/gauss.htm

12-12-2012, 01:46 PM   #6
johnsfine
Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,142

Rep:
Quote:
 Originally Posted by pan64 probably: printf("the answer is %20d", 99*100 + 500);
You might want to take another look at PTrenholme's answer.

12-12-2012, 02:00 PM   #7
johnsfine
Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,142

Rep:
Quote:
 Originally Posted by linosaurusroot I think Gauss's method is simpler.
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.

Last edited by johnsfine; 12-12-2012 at 02:02 PM.

12-12-2012, 02:09 PM   #8
pan64
Guru

Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,159

Rep:
Quote:
 Originally Posted by johnsfine You might want to take another look at PTrenholme's answer.
no, I know that already. I have just missed a zero, corrected

 12-12-2012, 02:39 PM #9 curious95 Member   Registered: Oct 2012 Location: /home/v Distribution: Slackware 14.0 Posts: 83 Original Poster Rep: So if the user was required to enter input the program could be similar to ... Code: ```#include 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; }```
12-12-2012, 02:43 PM   #10
johnsfine
Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,142

Rep:
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.

Last edited by johnsfine; 12-12-2012 at 02:54 PM.

12-12-2012, 02:48 PM   #11
curious95
Member

Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83

Original Poster
Rep:

Quote:
 Originally Posted by johnsfine 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;
}```
how's this?

12-12-2012, 02:55 PM   #12
johnsfine
Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,142

Rep:
Quote:
 Originally Posted by curious95 how's this?
Look at every step. You don't have the sequence right yet and I can't tell whether that is a concept issue or a carelessness issue.

Look at each variable that acts as input to any step. Does that variable have the intended value when that step is reached.

Last edited by johnsfine; 12-12-2012 at 02:57 PM.

 12-12-2012, 02:57 PM #13 curious95 Member   Registered: Oct 2012 Location: /home/v Distribution: Slackware 14.0 Posts: 83 Original Poster Rep: Right i'll reply when i have got the program fixed.
 12-12-2012, 03:01 PM #14 johnsfine Guru   Registered: Dec 2007 Distribution: Centos Posts: 5,142 Rep: 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). Last edited by johnsfine; 12-12-2012 at 03:06 PM.
 12-12-2012, 03:26 PM #15 curious95 Member   Registered: Oct 2012 Location: /home/v Distribution: Slackware 14.0 Posts: 83 Original Poster Rep: Here is the edited program seems i used L before declaring it. Code: ```#include 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; }```

 Tags math

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post knockout_artist Programming 7 11-25-2011 03:13 PM Ranbir Linux - Newbie 0 07-11-2009 11:04 PM phantom_cyph General 2 04-25-2007 03:59 AM Four General 5 04-19-2006 09:02 PM loke137 Programming 4 02-12-2004 08:12 AM

All times are GMT -5. The time now is 11:37 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -