Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. public class test {
  2.  
  3. // Computing the sum of the consecutive integers from 1 to n:
  4. long n = 10000; // Ten thousand
  5.  
  6. void algorithmA() {
  7. // Algorithm A
  8. long sum = 0;
  9. for (long i = 1; i <= n; i++)
  10. sum = sum + i;
  11. System.out.println(sum);
  12. }
  13.  
  14. void algorithmB()
  15. {
  16. // Algorithm B
  17. long sum = 0;
  18. for (long i = 1; 1 <= n; i++)
  19. {
  20. for (long j = 1; j <= 1; j++)
  21. sum = sum + 1;
  22. }
  23. System.out.println(sum);
  24. }
  25.  
  26. void algorithmC()
  27. {
  28. // Algorithm C
  29. long sum = n * (n + 1) / 2;
  30. System.out.println(sum);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement