View Full Version : my first java program!
inkedmn
06-01-2002, 08:00 PM
yes, that's right folks...
this code compiled and ran with no problems :)
public class Practice {
public static void main(String[] args) {
int x = 1;
for (int y = 1; y <= 100; y++) {
x += y;
}
System.out.println("Sum = " + x);
}
}
hooray!!!
/me goes to figure out how to use for loops and if statements... :)
Dru Lee Parsec
06-02-2002, 01:36 AM
Awesome dude!
Now I have a challenge for you. Do it without ANY loops.
OK, here's the algorithm. Let's say we're going to add the numbers from 1 to 10. We can add them this way:
1+2+3 . . . +10
Or we can add them this way
10+9+8+ . . . +1
and we get the same sum.
OK, so if we line up those two sequences like this:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
10+9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1
And add them all up then we would get a sum which is TWICE the sum of all the numbers from 1 to 10. So far so good right?
Now it doesn't matter what order we add things together, the sum will come out the same. So look at those 2 lines above and realize that the numbers that line up vertically all add up to 11. 1 + 10 = 11, 2 + 9 = 11 and so on.
So what we really have is 11 added together 10 times. Or we could say that if N = 10 then what we have is
(N + 1 ) * N
But that gives us a number which is twice as big as we want. How do we fix that? Divide by 2 of course. So for any integer N we can say that the sum of the numbers from 1 to N is
((N + 1 ) * N )/2
So now you have an optimized algorithm where if, for example, you wanted to add the numbers from 1 to 100,000 instead of looping 100,000 times you would do one addition, one multiplication, and one division.
Can you put that into code? You could try doing it both ways with different numbers and seeing if the answer comes out the same.
Bradmont
06-02-2002, 06:03 AM
Good old Gauss. :)
scanez
06-02-2002, 01:57 PM
P = 1+2+3+...+n = n(n+1)/2
Proof that P is true for all positive integers n: First we see that for n=1, 1 = 1(2)/2. Now assume P is true for some k. We show that it is true for k+1. Since P is true for k, we know that 1+2+...+k+k+1 = k(k+1)/2 + k+1 = k(k+1)+2(k+1)/2 = (k^2 + 3k + 2)/2 = (k+1)(k+2)/2. Hene it follows by induction that P is true for all positive integers n. QED
:)
DukeofNukes
06-02-2002, 03:27 PM
Discrete math makes my head hurt. Useful as hell for mathematical algorithms, but it's painful.
inkedmn
06-02-2002, 05:10 PM
dru-
here you go (thanks for your help, btw :)):
public class GetSum {
public static void main(String [] args) {
if (args.length == 0) {
System.out.println("Usage: GetSum [an integer]");
System.exit(0); }
long x = 0;
try {
x = Long.parseLong(args[0]);
}
catch(NumberFormatException nfe) {
System.out.println("That is not a valid number");
System.exit(0); }
long time1 = System.currentTimeMillis();
long sum = (( x + 1 ) * x) / 2;
long time2 = System.currentTimeMillis();
long delta = time2 - time1;
System.out.println("It took " + delta + " milliseconds.");
System.out.println("Sum: " + sum);
}
}
also prints out the calculation time :)
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.