Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class test {
- // Computing the sum of the consecutive integers from 1 to n:
- long n = 10000; // Ten thousand
- void algorithmA() {
- // Algorithm A
- long sum = 0;
- for (long i = 1; i <= n; i++)
- sum = sum + i;
- System.out.println(sum);
- }
- void algorithmB()
- {
- // Algorithm B
- long sum = 0;
- for (long i = 1; 1 <= n; i++)
- {
- for (long j = 1; j <= 1; j++)
- sum = sum + 1;
- }
- System.out.println(sum);
- }
- void algorithmC()
- {
- // Algorithm C
- long sum = n * (n + 1) / 2;
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement