Upcoming SlideShare
×

# Algorithms with-java-1.0

3,313 views
3,128 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
3,313
On SlideShare
0
From Embeds
0
Number of Embeds
1,356
Actions
Shares
0
116
0
Likes
0
Embeds 0
No embeds

No notes for slide
• ### Algorithms with-java-1.0

1. 1. Solve problems with Java code Algorithms with Java
2. 2. Sum 1..N – Example <ul><li>Calculate and print the sum of the first N positive numbers </li></ul>Scanner input = new Scanner(System.in); System.out.print(&quot;n = &quot;); int n = input.nextInt(); int num = 1; int sum = 1; System.out.print(&quot;The sum 1&quot;); while (num < n) { num++; sum += num; System.out.printf(&quot;+%d&quot;, num); } System.out.printf(&quot; = %d%n&quot;, sum);
3. 3. Calculating Sum 1..N Live Demo
4. 4. Prime Number – Example <ul><li>Checking if a number is prime or not </li></ul>Scanner input = new Scanner(System.in); System.out.print(&quot;Enter a positive integer: &quot;); int num = input.nextInt(); int divider = 2; int maxDivider = (int) Math.sqrt(num); boolean prime = true; while (prime && (divider <= maxDivider)) { if (num % divider == 0) { prime = false; } divider++; } System.out.println(&quot;Prime? &quot; + prime);
5. 5. Checking If a Number Is Prime Live Demo
6. 6. Using break Operator <ul><li>break operator exits the inner-most loop </li></ul>Scanner input = new Scanner(System.in); int n = input.nextInt(); // &quot;long&quot; is the biggest integer type long factorial = 1; // Perform an infinite loop while (true) { if (n == 1) { break; } factorial *= n; n--; } System.out.println(&quot;n! = &quot; + factorial);
7. 7. Calculating Factorial Live Demo
8. 8. Factorial – Example <ul><li>Calculating N factorial </li></ul>Scanner input = new Scanner(System.in); System.out.print(&quot;n = &quot;); int n = input.nextInt(); long factorial = 1; do { factorial *= n; n--; } while (n > 0); System.out.println(&quot;n! = &quot; + factorial);
9. 9. Factorial (do ... while) Live Demo
10. 10. Recursion Calling a Method by Itself
11. 11. What is Recursion? <ul><li>Recursion is calling a method by itself </li></ul><ul><ul><li>Very powerful technique for implementing combinatorial algorithms </li></ul></ul><ul><li>Recursion should have </li></ul><ul><ul><li>Direct or indirect recursive calls </li></ul></ul><ul><ul><ul><li>The method calls itself directly or through other methods </li></ul></ul></ul><ul><ul><li>Exit criteria (bottom) </li></ul></ul><ul><ul><ul><li>Prevent infinite recursion </li></ul></ul></ul>
12. 12. Factorial – Example <ul><li>N! (N Factorial) </li></ul><ul><ul><li>N! = N * (N – 1)! for N >= 0 and 0! = 1 </li></ul></ul><ul><ul><li>5! = 5 * 4! or 5 * 4 * 3 * 2 * 1 * 1 = 120 </li></ul></ul><ul><ul><li>4! = 4 * 3! or 4 * 3 * 2 * 1 * 1 = 24 </li></ul></ul><ul><ul><li>3! = 3 * 2! or 3 * 2 * 1 * 1 = 6 </li></ul></ul><ul><ul><li>2! = 2 * 1! or 2 * 1 * 1 = 2 </li></ul></ul><ul><ul><li>1! = 1 * 0! or 1 * 1 = 1 </li></ul></ul><ul><ul><li>0! = 1 </li></ul></ul>
13. 13. Factorial – Example <ul><li>Calculating factorial: </li></ul><ul><ul><li>0! = 1 </li></ul></ul><ul><ul><li>n! = n* (n-1)!, n>0 </li></ul></ul><ul><li>Don't do this at home! </li></ul><ul><ul><li>Use iteration instead </li></ul></ul>Recursive call: the method calls itself The bottom of the recursion public static int f actorial(int n) { if (n == 0) return 1; else return n * f actorial(n - 1); }
14. 14. Product[N..M] – Example <ul><li>Calculating the product of all numbers in the interval [n..m]: </li></ul>int n = input.nextInt(); int m = input.nextInt(); int num = n; long product = 1; do { product *= num; num++; } while(num <= m); System.out.println(&quot;product[n..m] = &quot; + product);
15. 15. Product of the Numbers in the Interval [n..m] Live Demo
16. 16. N^M – Example <ul><li>Calculating n^m </li></ul>Scanner input = new Scanner(System.in); int n = input.nextInt(); int m = input.nextInt(); long result = 1; for (int i = 0; i < m; i++) { result *= n; } System.out.println(&quot;n^m = &quot; + result);
17. 17. Calculating N^M Live Demo
18. 18. Using continue Operator <ul><li>continue operator ends iteration of the inner-most loop </li></ul><ul><li>Example: Sum odd numbers p in [1, n] that are not divisors of 7: </li></ul>int n = input.nextInt(); int sum = 0; for (int i = 1; i <= n; i += 2) { if (i % 7 == 0) continue; sum += i; } System.out.println(&quot;sum = &quot; + sum);
19. 19. Using continue Operator Live Demo
20. 20. Nested Loops Using Loop Inside a Loop
21. 21. What Is Nested Loop? <ul><li>A composition of loops is called a nested loop </li></ul><ul><li>Example: </li></ul>for (initialization; test; update) { for (initialization; test; update) { statements; } … }
22. 22. Nested Loops Examples
23. 23. Triangle – Example <ul><li>Print the following triangle on the console: </li></ul><ul><li>1 </li></ul><ul><li>1 2 </li></ul><ul><li>… </li></ul><ul><li>1 2 3 ... n </li></ul>int n = input.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + &quot; &quot;); } System.out.println(); }
24. 24. Triangle Live Demo
25. 25. Primes[N, M] – Example <ul><li>Print all prime numbers in [n, m] </li></ul>int n = input.nextInt(); int m = input.nextInt(); for (int num = n; num <= m; num++) { boolean prime = true; int divider = 2; int maxDivider = (int) Math.sqrt(num); while (divider <= maxDivider) { if (num % divider == 0) { prime = false; break; } divider++; } if (prime) { System.out.printf(&quot;%d &quot;, num); } }
26. 26. Primes in Range [n, m] Live Demo
27. 27. Loops – More Examples
28. 28. Nested Loops – Examples <ul><li>Print all four digit numbers ABCD such that A+B = C+D (happy numbers) </li></ul>for (int a = 1; a <= 9; a++) { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { for (int d = 0; d <= 9; d++) { if ((a + b) == (c + d)) { System.out.printf(&quot;%d,%d,%d,%d&quot;, a, b, c, d); } } } } } Can you improve this algorithm to use only 3 loops?
29. 29. Happy Numbers Live Demo
30. 30. TOTO 6/49 – Examples <ul><li>Print all combinations from TOTO 6/49 </li></ul>for (int i1 = 1; i1 <= 44; i1++) for (int i2 = i1 + 1; i2 <= 45; i2++) for (int i3 = i2 + 1; i3 <= 46; i3++) for (int i4 = i3 + 1; i4 <= 47; i4++) for (int i5 = i4 + 1; i5 <= 48; i5++) for (int i6 = i5 + 1; i6 <= 49; i6++) System.out.printf( &quot;%d %d %d %d %d %d%n&quot;, i1, i2, i3, i4, i5, i6); How long will it take to finish this?
31. 31. TOTO 6/49 Live Demo
32. 32. Summary <ul><li>Loops could solve different problems </li></ul><ul><li>Recursion could be handy as well </li></ul><ul><li>We can use nested loops to implement more complex logic </li></ul><ul><li>We can use continue and break operators to control the loop execution </li></ul><ul><li>More to come with arrays' manipulation </li></ul>
33. 33. Exercises <ul><li>Write a program that prints all the numbers from 1 to N. </li></ul><ul><li>Write a program that prints all the numbers from 1 to N, that are not divisible by 3 and 7. </li></ul><ul><li>Write a program that reads from the console a sequence of N integer numbers and returns the minimal and maximal of them. </li></ul><ul><li>Write a program that calculates N!/K! for given N and K (1<N<K). </li></ul>
34. 34. Exercises (3) <ul><li>Write a program that reads a number N and calculates the sum of the first N members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, … </li></ul><ul><li>In the combinatorial mathematics, the Catalan numbers are calculated by the following formula: </li></ul><ul><li>Write a program to calculate the Catalan number by given N. </li></ul>
35. 35. Exercises (4) <ul><li>Write a program that reads from the console a positive integer number N (N < 20) and outputs a matrix like the following: N = 3 N = 4 </li></ul>1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 1 2 3 2 3 4 3 4 5
36. 36. Exercises (5) <ul><li>Write a program that calculates for given N how many trailing zeros present at the end of the number N!. Examples: </li></ul><ul><li>N = 10  N! = 36288 00  2 </li></ul><ul><li>N = 20  N! = 243290200817664 0000  4 </li></ul><ul><li>Does your program work for N = 50 000? </li></ul><ul><li>Hint: The trailing zeros in N! are equal to the number of its prime divisors 5. Think why! </li></ul>