// Helene Martin, CSE 142 // Demonstrates cumulative algorithms. public class CumulativeSum { public static void main(String[] args) { // add up all numbers from 1 - 1000 //int total = 1 + 2 + 3 + 4 + 5 + 6 + 7... int total = 0; for (int i = 1; i <= 1000; i++) { total += i; // same as total = total + i; } System.out.println(total); System.out.println("Our method: " + pow(2, 20)); System.out.println("Math method: " + Math.pow(2, 20)); } // returns the result of base to the power of exponent public static int pow(int base, int exponent) { int result = 1; for (int i = 0; i < 20; i++) { result *= 2; // same as result = result * 2; } return result; } }