Download presentation

Presentation is loading. Please wait.

Published byAbigail Boyd Modified over 3 years ago

1
Solve problems with Java code Algorithms with Java

2
Sum 1..N – Example Calculate and print the sum of the first N positive numbers Calculate and print the sum of the first N positive numbers 2 Scanner input = new Scanner(System.in); System.out.print("n = "); int n = input.nextInt(); int num = 1; int sum = 1; System.out.print("The sum 1"); while (num < n) { num++; num++; sum += num; sum += num; System.out.printf("+%d", num); System.out.printf("+%d", num);} System.out.printf(" = %d%n", sum);

3
Calculating Sum 1..N Live Demo

4
Prime Number – Example Checking if a number is prime or not Checking if a number is prime or not 4 Scanner input = new Scanner(System.in); System.out.print("Enter a positive integer: "); int num = input.nextInt(); int divider = 2; int maxDivider = (int) Math.sqrt(num); boolean prime = true; while (prime && (divider <= maxDivider)) { if (num % divider == 0) { if (num % divider == 0) { prime = false; prime = false; } divider++; divider++;} System.out.println("Prime? " + prime);

5
Checking If a Number Is Prime Live Demo

6
Using break Operator break operator exits the inner-most loop break operator exits the inner-most loop 6 Scanner input = new Scanner(System.in); int n = input.nextInt(); // "long" is the biggest integer type long factorial = 1; // Perform an infinite loop while (true) { if (n == 1) { if (n == 1) { break; break; } factorial *= n; factorial *= n; n--; n--;} System.out.println("n! = " + factorial);

7
Calculating Factorial Live Demo

8
Factorial – Example Calculating N factorial Calculating N factorial 8 Scanner input = new Scanner(System.in); System.out.print("n = "); int n = input.nextInt(); long factorial = 1; do { factorial *= n; factorial *= n; n--; n--;} while (n > 0); System.out.println("n! = " + factorial);

9
Factorial (do... while) Live Demo

10
Recursion Calling a Method by Itself

11
What is Recursion? Recursion is calling a method by itselfRecursion is calling a method by itself Very powerful technique for implementing combinatorial algorithmsVery powerful technique for implementing combinatorial algorithms Recursion should haveRecursion should have Direct or indirect recursive callsDirect or indirect recursive calls The method calls itself directly or through other methodsThe method calls itself directly or through other methods Exit criteria (bottom)Exit criteria (bottom) Prevent infinite recursionPrevent infinite recursion

12
Factorial – Example N! (N Factorial)N! (N Factorial) N! = N * (N – 1)! for N >= 0 and 0! = 1N! = N * (N – 1)! for N >= 0 and 0! = 1 5! = 5 * 4! or 5 * 4 * 3 * 2 * 1 * 1 = 1205! = 5 * 4! or 5 * 4 * 3 * 2 * 1 * 1 = 120 4! = 4 * 3! or 4 * 3 * 2 * 1 * 1 = 244! = 4 * 3! or 4 * 3 * 2 * 1 * 1 = 24 3! = 3 * 2! or 3 * 2 * 1 * 1 = 63! = 3 * 2! or 3 * 2 * 1 * 1 = 6 2! = 2 * 1! or 2 * 1 * 1 = 22! = 2 * 1! or 2 * 1 * 1 = 2 1! = 1 * 0! or 1 * 1 = 11! = 1 * 0! or 1 * 1 = 1 0! = 10! = 1

13
Factorial – Example Calculating factorial:Calculating factorial: 0! = 10! = 1 n! = n* (n-1)!, n>0n! = n* (n-1)!, n>0 Don't do this at home!Don't do this at home! Use iteration insteadUse iteration instead Recursive call: the method calls itself The bottom of the recursion public static int factorial(int n) { if (n == 0) return 1; else return 1; else return n * factorial(n - 1); } return n * factorial(n - 1); }

14
Product[N..M] – Example Calculating the product of all numbers in the interval [n..m]: Calculating the product of all numbers in the interval [n..m]: 14 int n = input.nextInt(); int m = input.nextInt(); int num = n; long product = 1; do { product *= num; product *= num; num++; num++;} while(num <= m); System.out.println("product[n..m] = " + product);

15
Product of the Numbers in the Interval [n..m] Live Demo

16
N^M – Example Calculating n^m Calculating n^m 16 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; result *= n;} System.out.println("n^m = " + result);

17
Calculating N^M Live Demo

18
Using continue Operator continue operator ends iteration of the inner-most loop continue operator ends iteration of the inner-most loop Example: Sum odd numbers p in [1, n] that are not divisors of 7: Example: Sum odd numbers p in [1, n] that are not divisors of 7: 18 int n = input.nextInt(); int sum = 0; for (int i = 1; i <= n; i += 2) { if (i % 7 == 0) if (i % 7 == 0) continue; continue; sum += i; sum += i;} System.out.println("sum = " + sum);

19
Using continue Operator Live Demo

20
Nested Loops Using Loop Inside a Loop

21
What Is Nested Loop? A composition of loops is called a nested loop A composition of loops is called a nested loop Example: Example: 21 for (initialization; test; update) { for (initialization; test; update) { for (initialization; test; update) { statements; statements; } …}

22
Nested Loops Examples

23
Triangle – Example Print the following triangle on the console: Print the following triangle on the console:1 1 2 … n 23 int n = input.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); System.out.print(j + " "); } System.out.println(); System.out.println();}

24
Triangle Live Demo

25
Primes[N, M] – Example Print all prime numbers in [n, m] Print all prime numbers in [n, m] 25 int n = input.nextInt(); int m = input.nextInt(); for (int num = n; num <= m; num++) { boolean prime = true; boolean prime = true; int divider = 2; int divider = 2; int maxDivider = (int) Math.sqrt(num); int maxDivider = (int) Math.sqrt(num); while (divider <= maxDivider) { while (divider <= maxDivider) { if (num % divider == 0) { if (num % divider == 0) { prime = false; prime = false; break; break; } divider++; divider++; } if (prime) { if (prime) { System.out.printf("%d ", num); System.out.printf("%d ", num); }}

26
Primes in Range [n, m] Live Demo

27
Loops – More Examples

28
Nested Loops – Examples Print all four digit numbers ABCD such that A+B = C+D (happy numbers) Print all four digit numbers ABCD such that A+B = C+D (happy numbers) 28 for (int a = 1; a <= 9; a++) { for (int b = 0; b <= 9; b++) { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { for (int c = 0; c <= 9; c++) { for (int d = 0; d <= 9; d++) { for (int d = 0; d <= 9; d++) { if ((a + b) == (c + d)) { if ((a + b) == (c + d)) { System.out.printf("%d,%d,%d,%d", System.out.printf("%d,%d,%d,%d", a, b, c, d); a, b, c, d); }} } }} Can you improve this algorithm to use only 3 loops?

29
Happy Numbers Live Demo

30
TOTO 6/49 – Examples Print all combinations from TOTO 6/49 Print all combinations from TOTO 6/49 30 for (int i1 = 1; i1 <= 44; i1++) for (int i2 = i1 + 1; i2 <= 45; i2++) for (int i2 = i1 + 1; i2 <= 45; i2++) for (int i3 = i2 + 1; i3 <= 46; i3++) for (int i3 = i2 + 1; i3 <= 46; i3++) for (int i4 = i3 + 1; i4 <= 47; i4++) for (int i4 = i3 + 1; i4 <= 47; i4++) for (int i5 = i4 + 1; i5 <= 48; i5++) for (int i5 = i4 + 1; i5 <= 48; i5++) for (int i6 = i5 + 1; i6 <= 49; i6++) for (int i6 = i5 + 1; i6 <= 49; i6++) System.out.printf( System.out.printf( "%d %d %d %d %d %d%n", "%d %d %d %d %d %d%n", i1, i2, i3, i4, i5, i6); i1, i2, i3, i4, i5, i6); How long will it take to finish this?

31
TOTO 6/49 Live Demo

32
Summary Loops could solve different problems Loops could solve different problems Recursion could be handy as well Recursion could be handy as well We can use nested loops to implement more complex logic We can use nested loops to implement more complex logic We can use continue and break operators to control the loop execution We can use continue and break operators to control the loop execution More to come with arrays' manipulation More to come with arrays' manipulation 32

33
Exercises 1.Write a program that prints all the numbers from 1 to N. 2.Write a program that prints all the numbers from 1 to N, that are not divisible by 3 and 7. 3.Write a program that reads from the console a sequence of N integer numbers and returns the minimal and maximal of them. 4.Write a program that calculates N!/K! for given N and K (1

34
Exercises (3) 1.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, … 2.In the combinatorial mathematics, the Catalan numbers are calculated by the following formula: Write a program to calculate the Catalan number by given N. 34

35
Exercises (4) 1. Write a program that reads from the console a positive integer number N (N < 20) and outputs a matrix like the following: N = 3N =

36
Exercises (5) Write a program that calculates for given N how many trailing zeros present at the end of the number N!. Examples: N = 10 N! = N = 20 N! = Does your program work for N = ? Hint: The trailing zeros in N! are equal to the number of its prime divisors 5. Think why!

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google