Conditional%20assignment - PowerPoint PPT Presentation

About This Presentation
Title:

Conditional%20assignment

Description:

A fast way to compute the greatest common divisor of two numbers a and b. Algorithm (this is not a program) ... Primality testing. class primalityTestSlow ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 11
Provided by: RMK
Category:

less

Transcript and Presenter's Notes

Title: Conditional%20assignment


1
Conditional assignment
  • if ((x2)0)
  • y x/2
  • else
  • y (x1)/2
  • Same as
  • y ((x2)0) ? x/2 (x1)/2

2
Euclids division algorithm
  • A fast way to compute the greatest common divisor
    of two numbers a and b
  • Algorithm (this is not a program)
  • Divide b by a, assign a to b and the remainder to
    a loop until a is zero. The value of b at this
    point is the gcd.
  • Assumes a is less than b
  • Main observation gcd (a, b) gcd (a, b-a)

3
GCD
  • class gcd
  • public static void main (String arg)
  • int a 40, b 24, r, gcd
  • if ((a0) (b0))
  • gcd (a gt b) ? a b
  • else if (a lt b)
  • while (a!0)
  • r ba
  • b a
  • a r
  • gcd b
  • // next slide

4
GCD
  • else
  • while (b!0)
  • r ab
  • a b
  • b r
  • gcd a
  • System.out.println(GCD gcd)

5
Sorting three numbers
  • class sortThree
  • public static void main(String arg)
  • int x 2, y 5, z 1
  • int max, mid, min
  • if ((x gt y) (x gt z))
  • max x
  • if (y gt z)
  • mid y
  • min z
  • else
  • mid z
  • min y
  • // next slide

6
Sorting three numbers
  • else
  • if (y gt z)
  • max y
  • if (x gt z)
  • mid x
  • min z
  • else
  • mid z
  • min x
  • else // the remaining two
    permutations
  • // end else
  • // end main
  • // end class

7
Integer index
  • class integerIndex
  • public static void main(String arg)
  • int n 3
  • double x 3.14, y 1.0
  • int m n
  • if (n lt 0)
  • x 1/x
  • m -n
  • while (m gt 0)
  • y x
  • m--
  • System.out.println(x to the power
    n is y)

8
Positive Decimal to Binary
  • class positiveDecimalToBinary
  • public static void main(String arg)
  • int n 34, y0, polyTerm 1
  • if (n lt 0)
  • System.out.println(Sorry, cannot
    handle negative integers today!)
  • else
  • while (n gt 0)
  • y (polyTerm(n2))
  • n / 2
  • polyTerm 10
  • System.out.println(Required binary
    y)

9
Primality testing
  • class primalityTestSlow
  • public static void main (String arg)
  • int n 42, d
  • if (n lt 1)
  • System.out.println(n is not a
    prime.)
  • else
  • for (d2 dltn/2 d)
  • if ((nd)0)
  • System.out.println(n is
    not a prime.)
  • break
  • if (d gt n/2)
  • System.out.println(n is a
    prime.)

10
Primality testing
  • class primalityTestLittleBetter
  • public static void main (String arg)
  • int n 42, d
  • if (n lt 1)
  • System.out.println(n is not a
    prime.)
  • else
  • for (d2 ddltn d)
  • if ((nd)0)
  • System.out.println(n is
    not a prime.)
  • break
  • if (dd gt n)
  • System.out.println(n is a
    prime.)
Write a Comment
User Comments (0)
About PowerShow.com