Fun With Number Systems Or: How Knowing Number Systems Can Help You - - PowerPoint PPT Presentation

fun with number systems
SMART_READER_LITE
LIVE PREVIEW

Fun With Number Systems Or: How Knowing Number Systems Can Help You - - PowerPoint PPT Presentation

Fun With Number Systems Or: How Knowing Number Systems Can Help You Interview Better Finding the Odd Ball 1 2 9 10 11 3 3 2 1 10 11 9 Goal: Find the odd ball and whether it's heavier or lighter in three weighings. The Solution 5 6


slide-1
SLIDE 1

Fun With Number Systems

Or: How Knowing Number Systems Can Help You Interview Better

slide-2
SLIDE 2

Finding the Odd Ball

slide-3
SLIDE 3
slide-4
SLIDE 4

9 10 11 1 2 3

slide-5
SLIDE 5

9 10 11 1 2 3

slide-6
SLIDE 6

Goal: Find the odd ball and whether it's heavier or lighter in three weighings.

slide-7
SLIDE 7

The Solution

5 6 7 11 8 9 10 12 2 3 4 11 5 6 7 12 1 4 7 8 2 5 10 11

slide-8
SLIDE 8

Balanced Ternary

  • Number system for encoding three-way

comparisons.

  • Each digit corresponds to a power of three.
  • Digits are -1, 0, +1.
  • For notational simplicity, will use -, 0, +.
  • Example: +0-0
  • 1 x 33 + 0 x 32 – 1 x 31 + 0 x 30 = 24
  • Example: --++
  • -1 x 33 – 1 x 32 + 1 x 31 + 1 x 30 = -32
slide-9
SLIDE 9
  • 12 to +12 in Balanced Ternary

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

000

slide-10
SLIDE 10

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

slide-11
SLIDE 11

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

slide-12
SLIDE 12

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

slide-13
SLIDE 13

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10

slide-14
SLIDE 14

1 2 3 4 5 6 7 8 9 10 11 12

1 4 7 10

00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0

slide-15
SLIDE 15

1 2 3 4 5 6 7 8 9 10 11 12

1 4 7 10

00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0

2 5 8 11

slide-16
SLIDE 16

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

slide-17
SLIDE 17

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

slide-18
SLIDE 18

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

??+

slide-19
SLIDE 19

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

slide-20
SLIDE 20

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

??-

slide-21
SLIDE 21

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

slide-22
SLIDE 22

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

??0

slide-23
SLIDE 23

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12
slide-24
SLIDE 24

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12
slide-25
SLIDE 25

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

??-

slide-26
SLIDE 26

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12
slide-27
SLIDE 27

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

??+

slide-28
SLIDE 28

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12
slide-29
SLIDE 29

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

1 4 7 10 2 5 8 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

??0

slide-30
SLIDE 30

If the left side is heavier, record a +. If the right side is heavier, record a -. If the scale balances, record a 0.

slide-31
SLIDE 31

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

slide-32
SLIDE 32

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

2 3 4 11 12

slide-33
SLIDE 33

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

2 3 4 11 12

slide-34
SLIDE 34

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

2 3 4 11 5 6 7 12

slide-35
SLIDE 35

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +0- +00 +0+ ++- ++0 12

2 3 4 11 5 6 7 12

+-- +-0 +-+

slide-36
SLIDE 36

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +0- +00 +0+ ++- ++0 12

2 3 4 11 5 6 7 12

+-- +-0 +-+

slide-37
SLIDE 37

Our Encoding Scheme

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

Lighter Heavier

slide-38
SLIDE 38

Our Encoding Scheme

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

Lighter Heavier

slide-39
SLIDE 39

Our Encoding Scheme

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+

Lighter Heavier

  • -0
  • 12
slide-40
SLIDE 40

Our Encoding Scheme

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+

Lighter Heavier

  • -0
  • 12
slide-41
SLIDE 41

Our Encoding Scheme

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+

Lighter Heavier

  • -0
  • 12
slide-42
SLIDE 42

Our Encoding Scheme

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • -+

Lighter Heavier

  • -0
  • 12
  • 8
  • 9
  • 10
  • 0+
  • 00
  • 0-
slide-43
SLIDE 43

Our Encoding Scheme

1 2 3 4 5 6 7 11 00+ 0+- 0+0 0++ +-- +-0 +-+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • -+

Lighter Heavier

  • -0
  • 12

8 9 10 +0- +00 +0+

  • 8
  • 9
  • 10
  • 0+
  • 00
  • 0-
slide-44
SLIDE 44

The Solution

5 6 7 11 8 9 10 12 2 3 4 11 5 6 7 12 1 4 7 8 2 5 10 11

1 2 3 4 5 6 7 11 00+ 0+- 0+0 0++ +-- +-0 +-+ ++-

  • -0
  • 12
  • 8
  • 9
  • 10
  • 0+
  • 00
  • 0-
slide-45
SLIDE 45

Generalizing the Result

  • Why twelve balls?
  • With three trits, 27 possible combinations. 13 are positive, nine

start with +.

  • Must discard one starting with + to ensure number of + and - in

each column is the same, leaving 12 positive numbers.

  • More generally:
  • With n trits, 3n possible combinations. (3n – 1) / 2 are positive.
  • 3n-1 numbers start with +. To balance + and -, we need to drop
  • ne starting with +, leaving (3n – 3) / 2 positive numbers.
  • Can do 3, 12, 39, 120, 363, 1092, ...
slide-46
SLIDE 46

1 2 3 0+ +- +0 4 ++

Example: Two Weighings

  • 1

0-

  • 2
  • +
  • 4
  • 3

Lighter Heavier

slide-47
SLIDE 47

1 2 3 0+ +- +0 4 ++

Example: Two Weighings

  • 1

0-

  • 2
  • +
  • 4
  • 3

Lighter Heavier

slide-48
SLIDE 48

1 2 3 0+ +- +0

Example: Two Weighings

  • 1

0-

  • 2
  • +
  • 3

Lighter Heavier

slide-49
SLIDE 49

1 2 3 0+ +- +0

Example: Two Weighings

  • 1

0-

  • 2
  • +
  • 3

Lighter Heavier

slide-50
SLIDE 50

1 2 3 0+ +- +0

Example: Two Weighings

  • 1

0-

  • 2
  • +
  • 3

Lighter Heavier

slide-51
SLIDE 51

1 2 3 0+ +- +0

Example: Two Weighings

  • 1

0-

  • 2
  • +
  • 3

Lighter Heavier

slide-52
SLIDE 52

Example: Three Weighings

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

Lighter Heavier +++ 13

  • 13
slide-53
SLIDE 53

Example: Three Weighings

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

Lighter Heavier +++ 13

  • 13
slide-54
SLIDE 54

Example: Three Weighings

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+
  • -0
  • 12

Lighter Heavier

slide-55
SLIDE 55

Example: Three Weighings

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++-

  • -0
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • 0+
  • 00
  • 0-
  • -+

Lighter Heavier ++0 12

slide-56
SLIDE 56

Example: Three Weighings

1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • -+

Lighter Heavier

  • -0
  • 12
  • 8
  • 9
  • 10
  • 0+
  • 00
  • 0-
slide-57
SLIDE 57

Example: Three Weighings

1 2 3 4 5 6 7 11 00+ 0+- 0+0 0++ +-- +-0 +-+ ++- ++0 12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 11

00- 0-+ 0-0 0--

  • ++
  • +0
  • +-
  • -+

Lighter Heavier

  • -0
  • 12

8 9 10 +0- +00 +0+

  • 8
  • 9
  • 10
  • 0+
  • 00
  • 0-
slide-58
SLIDE 58

Example: Four Weighings

1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22 23 24 +--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++ +0-- +0-0 25 +0-+ 26 27 28 29 30 31 32 33 34 35 36 37 +000 +00+ +0+- +0+0 +0++ ++-- ++-0 ++-+ ++0- ++00 ++0+ +++- 38 +++0 39 +00- ++++ 40

slide-59
SLIDE 59

Example: Four Weighings

1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22 23 24 +--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++ +0-- +0-0 25 +0-+ 26 27 28 29 30 31 32 33 34 35 36 37 +000 +00+ +0+- +0+0 +0++ ++-- ++-0 ++-+ ++0- ++00 ++0+ +++- 38 +++0 39 +00- ++++ 40

slide-60
SLIDE 60

Example: Four Weighings

1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22 23 24 +--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++ +0-- +0-0 25 +0-+ 26 27 28 29 30 31 32 33 34 35 36 37 +000 +00+ +0+- +0+0 +0++ ++-- ++-0 ++-+ ++0- ++00 ++0+ +++- 38 +++0 39 +00-

slide-61
SLIDE 61

Example: Four Weighings

1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22 23 24 +--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++ +0-- +0-0 25 +0-+ 26 27 28 29 30 31 32 33 34 35 36 37 +000 +00+ +0+- +0+0 +0++ ++-- ++-0 ++-+ ++0- ++00 ++0+ +++- 38 +++0 39 +00-

slide-62
SLIDE 62

Example: Four Weighings

1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22

  • 23
  • 24

+--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++

  • 0++
  • 0+0
  • 25
  • 0+-
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

32 33 34

  • 35
  • 36
  • 37
  • 000
  • 00-
  • 0-+
  • 0-0
  • 0--

++-- ++-0 ++-+

  • -0+
  • -00
  • -0-

+++- 38

  • --0
  • 39
  • 00+
slide-63
SLIDE 63

Example: Four Weighings

1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22

  • 23
  • 24

+--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++

  • 0++
  • 0+0
  • 25
  • 0+-
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

32 33 34

  • 35
  • 36
  • 37
  • 000
  • 00-
  • 0-+
  • 0-0
  • 0--

++-- ++-0 ++-+

  • -0+
  • -00
  • -0-

+++- 38

  • --0
  • 39
  • 00+
slide-64
SLIDE 64

Some Insights

  • What number did we drop?
  • With 2 trits, dropped ++.
  • With 3 trits, dropped +++.
  • With 4 trits, dropped ++++.
  • Always drop ++…++
  • What numbers did we invert?
  • With two trits: +0
  • With three trits: ++0, +0-, +00, +0-
  • With four trits: +++0, ++0-, ++00, ++0+, +0--, +0-0, +0-+, +00-,

+000, +00+, +0+-, +0+0, +0++

  • Always invert numbers starting with ++…++0.
  • This always works!
slide-65
SLIDE 65

How Many Extra +s Per Column?

  • Answer: The 3j column has (3j – 1) / 2 extra +'s.

0+ +- +0

slide-66
SLIDE 66

How Many Extra +s Per Column?

  • Answer: The 3j column has (3j – 1) / 2 extra +'s.

00+ 0+- 0+0

slide-67
SLIDE 67

How Many Extra +s Per Column?

  • Answer: The 3j column has (3j – 1) / 2 extra +'s.

00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0

slide-68
SLIDE 68

How Many Extra +s Per Column?

  • Answer: The 3j column has (3j – 1) / 2 extra +'s.

00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0

slide-69
SLIDE 69

How Many +'s Get Flipped?

  • Answer: The 3j column has (3j-1) / 2 flipped +'s.
slide-70
SLIDE 70

How Many +'s Get Flipped?

  • Answer: The 3j column has (3j-1) / 2 flipped +'s.

0+ +- +0

slide-71
SLIDE 71

How Many +'s Get Flipped?

  • Answer: The 3j column has (3j-1) / 2 flipped +'s.

0+ +- +0

slide-72
SLIDE 72

How Many +'s Get Flipped?

  • Answer: The 3j column has (3j-1) / 2 flipped +'s.

00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0

slide-73
SLIDE 73

How Many +'s Get Flipped?

  • Answer: The 3j column has (3j-1) / 2 flipped +'s.

00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0

slide-74
SLIDE 74

How Many +'s Get Flipped?

  • Answer: The 3j column has (3j-1) / 2 flipped +'s.

00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0

i=0 j−1

3

i=3 j−1

2

slide-75
SLIDE 75

Summary

  • A three-way scale lends itself naturally to a

balanced ternary encoding for each of the balls.

  • Given an encoding with the same number of +'s

and -'s in each column, we can use the scale to read off one trit of the answer at a time.

  • Flipping numbers starting with +0, ++0, etc.

guarantees an encoding with this property.

slide-76
SLIDE 76

Generating Permutations

slide-77
SLIDE 77

“You are given a sorted string S of unique

  • characters. Write a Java-style iterator that

traverses all the permutations of S in lexicographical order.”

slide-78
SLIDE 78

Example

slide-79
SLIDE 79

Example

abc

slide-80
SLIDE 80

Example

abc acb bac bca cab cba

slide-81
SLIDE 81

Example

abc acb bac bca cab cba 1 2 3 4 5

slide-82
SLIDE 82

Lehmer Codes

B A E D C

slide-83
SLIDE 83

Lehmer Codes

B A E D C

slide-84
SLIDE 84

Lehmer Codes

B A E D C

slide-85
SLIDE 85

1

Lehmer Codes

B A E D C

slide-86
SLIDE 86

1

Lehmer Codes

B A E D C

slide-87
SLIDE 87

1

Lehmer Codes

B A E D C

slide-88
SLIDE 88

1

Lehmer Codes

B A E D C

slide-89
SLIDE 89

1

Lehmer Codes

B A E D C

slide-90
SLIDE 90

1

Lehmer Codes

B A E D C

slide-91
SLIDE 91

1

Lehmer Codes

B A E D C

slide-92
SLIDE 92

2 1

Lehmer Codes

B A E D C

slide-93
SLIDE 93

2 1

Lehmer Codes

B A E D C

slide-94
SLIDE 94

2 1

Lehmer Codes

B A E D C

slide-95
SLIDE 95

2 1

Lehmer Codes

B A E D C

slide-96
SLIDE 96

1 2 1

Lehmer Codes

B A E D C

slide-97
SLIDE 97

1 2 1

Lehmer Codes

B A E D C

slide-98
SLIDE 98

1 2 1

Lehmer Codes

B A E D C

slide-99
SLIDE 99

1 2 1

Lehmer Codes

B A E D C

slide-100
SLIDE 100

1 2 1

Lehmer Codes

B A E D C

slide-101
SLIDE 101

1 2 1

Lehmer Codes

B A E D C C D A B E

slide-102
SLIDE 102

1 2 1

Lehmer Codes

B A E D C 2 C D A B E

slide-103
SLIDE 103

2 1 2 1

Lehmer Codes

B A E D C 2 C D A B E

slide-104
SLIDE 104

2 1 2 1

Lehmer Codes

B A E D C 2 C D A B E

slide-105
SLIDE 105

2 1 2 1

Lehmer Codes

B A E D C 2 C D A B E

slide-106
SLIDE 106

2 1 2 1

Lehmer Codes

B A E D C 2 C D A B E

slide-107
SLIDE 107

4 1 1

Lehmer Codes

A B C D E

slide-108
SLIDE 108

4 1 1

Lehmer Codes

A B C D E

slide-109
SLIDE 109

4 1 1

Lehmer Codes

A B C D E

slide-110
SLIDE 110

4 1 1

Lehmer Codes

A B C D E

slide-111
SLIDE 111

4 1 1

Lehmer Codes

A B C D E

slide-112
SLIDE 112

4 1 1

Lehmer Codes

A B C D E

slide-113
SLIDE 113

4 1 1

Lehmer Codes

A B C D E

slide-114
SLIDE 114

4 1 1

Lehmer Codes

A B C D E

slide-115
SLIDE 115

4 1 1

Lehmer Codes

A B C D E

slide-116
SLIDE 116

4 1 1

Lehmer Codes

A B C D E

slide-117
SLIDE 117

4 1 1

Lehmer Codes

A B C D E

slide-118
SLIDE 118

4 1 1

Lehmer Codes

A B C D E

slide-119
SLIDE 119

4 1 1

Lehmer Codes

A B C D E

slide-120
SLIDE 120

4 1 1

Lehmer Codes

A B C D E

slide-121
SLIDE 121

4 1 1

Lehmer Codes

A B C D E

slide-122
SLIDE 122

4 1 1

Lehmer Codes

A B C D E

slide-123
SLIDE 123

4 1 1

Lehmer Codes

A B C D E

slide-124
SLIDE 124

Listing Lehmer Codes

1 2 3 4 5 6 7 8 9 10 11 2000 2010 2100 2110 2200 2210 3000 3010 3100 3110 3200 3210 abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdac cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba 12 13 14 15 16 17 18 19 20 21 22 23 0000 0010 0100 0110 0200 0210 1000 1010 1100 1110 1200 1210

slide-125
SLIDE 125

Factoradic Numbers

  • Mixed-radix number system.
  • Nth digit in base n!.
  • Nth digit can be 0, 1, 2, …, n
  • Example: 3110!
  • 3 x 3! + 1 x 2! + 1 x 1! + 0 x 0! = 21
  • Example: 1210!
  • 1 x 3! + 2 x 2! + 1 x 1! + 0 x 0! = 11
slide-126
SLIDE 126

Listing Lehmer Codes

1 2 3 4 5 6 7 8 9 10 11 2000 2010 2100 2110 2200 2210 3000 3010 3100 3110 3200 3210 abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdac cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba 12 13 14 15 16 17 18 19 20 21 22 23 0000 0010 0100 0110 0200 0210 1000 1010 1100 1110 1200 1210

slide-127
SLIDE 127

Listing Lehmer Codes

1 2 3 4 5 6 7 8 9 10 11 2000 2010 2100 2110 2200 2210 3000 3010 3100 3110 3200 3210 abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdac cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba 12 13 14 15 16 17 18 19 20 21 22 23 0000 0010 0100 0110 0200 0210 1000 1010 1100 1110 1200 1210

slide-128
SLIDE 128

Writing n in factoradic gives the nth Lehmer code.

slide-129
SLIDE 129

Converting to Factoradic

  • Goal: convert k to factoradic.
  • Assume we know n, the number of elements to

permute.

  • To get the (n – 1)! place, divide k by (n – 1)!
  • Quotient is the (n – 1)! place.
  • Repeat for the remaining digits using the

remainder.

  • Identical to converting to any other base, just

using factorials instead of powers.

slide-130
SLIDE 130

Example: Convert 13 to Factoradic

slide-131
SLIDE 131

Example: Convert 13 to Factoradic

13 = 2 x 3! + 1

slide-132
SLIDE 132

Example: Convert 13 to Factoradic

13 = 2 x 3! + 1 1 = 0 x 2! + 1

slide-133
SLIDE 133

Example: Convert 13 to Factoradic

13 = 2 x 3! + 1 1 = 0 x 2! + 1 1 = 1 x 1! + 0

slide-134
SLIDE 134

Example: Convert 13 to Factoradic

13 = 2 x 3! + 1 1 = 0 x 2! + 1 1 = 1 x 1! + 0 0 = 0 x 0! + 0

slide-135
SLIDE 135

Example: Convert 13 to Factoradic

13 = 2 x 3! + 1 1 = 0 x 2! + 1 1 = 1 x 1! + 0 0 = 0 x 0! + 0

slide-136
SLIDE 136

Example: Convert 13 to Factoradic

13 = 2 x 3! + 1 1 = 0 x 2! + 1 1 = 1 x 1! + 0 0 = 0 x 0! + 0 Answer: 2010!

slide-137
SLIDE 137

Generating Permutations

public static String kthPermutation(String chars, int k) { String result = ""; for (int n = chars.length() - 1; n >= 0; --n) { int quotient = k / factorial(n); int remainder = k % factorial(n); result += chars.charAt(quotient); chars = chars.substring(0, quotient) + chars.substring(quotient + 1); k = remainder; } return result; }

slide-138
SLIDE 138

Analysis of Our Algorithm

  • To get the kth permutation of n elements:
  • Converting k to factoradic takes O(n)
  • Building up the permutations takes O(n2 )

– Elements stored in a list; O(n) to remove each.

  • Can reduce to O(n lg n) using order statistic tree.
  • Easy to build an iterator from this.
slide-139
SLIDE 139

Incrementing Binary Numbers

4

slide-140
SLIDE 140

Incrementing Binary Numbers

4 1

slide-141
SLIDE 141

Incrementing Binary Numbers

4 1

slide-142
SLIDE 142

Incrementing Binary Numbers

4 1

slide-143
SLIDE 143

Incrementing Binary Numbers

4 1 1

slide-144
SLIDE 144

Incrementing Binary Numbers

4 1 1

slide-145
SLIDE 145

Incrementing Binary Numbers

4 1

slide-146
SLIDE 146

Incrementing Factoradic Numbers

slide-147
SLIDE 147

Incrementing Factoradic Numbers

4

slide-148
SLIDE 148

Incrementing Factoradic Numbers

4

slide-149
SLIDE 149

Incrementing Factoradic Numbers

4 1

slide-150
SLIDE 150

Incrementing Factoradic Numbers

4 1

slide-151
SLIDE 151

Incrementing Factoradic Numbers

4 1

slide-152
SLIDE 152

Incrementing Factoradic Numbers

4 1

slide-153
SLIDE 153

Incrementing Factoradic Numbers

4 1 1

slide-154
SLIDE 154

Incrementing Factoradic Numbers

4 1 1

slide-155
SLIDE 155

Incrementing Factoradic Numbers

4 2

slide-156
SLIDE 156

Incrementing Factoradic Numbers

4 2

slide-157
SLIDE 157

Incrementing Factoradic Numbers

4 2 1

slide-158
SLIDE 158

Incrementing Factoradic Numbers

4 2 1

slide-159
SLIDE 159

Incrementing Factoradic Numbers

4 1

slide-160
SLIDE 160

Incrementing Factoradic Numbers

  • Find the digit to increment.
  • Scan backwards from the end to find the first

number not at its maximum.

  • Increment that digit.
  • Set the digits after that to zero.
slide-161
SLIDE 161

Incrementing Permutations

slide-162
SLIDE 162

4 B C D A

Incrementing Permutations

4

slide-163
SLIDE 163

4 B C D A

Incrementing Permutations

4

slide-164
SLIDE 164

4 B C D A

Incrementing Permutations

4 1

slide-165
SLIDE 165

4 B D C A

Incrementing Permutations

4 1

slide-166
SLIDE 166

4 B D C A

Incrementing Permutations

4 1

slide-167
SLIDE 167

4 B D C A

Incrementing Permutations

4 1

slide-168
SLIDE 168

4 B D C A

Incrementing Permutations

4 1 1

slide-169
SLIDE 169

4 C D B A

Incrementing Permutations

4 1 1

slide-170
SLIDE 170

4 C D B A

Incrementing Permutations

4 1

slide-171
SLIDE 171

4 C B D A

Incrementing Permutations

4 1

slide-172
SLIDE 172

4 C B D A

Incrementing Permutations

4 1

slide-173
SLIDE 173

4 C B D A

Incrementing Permutations

4 1

slide-174
SLIDE 174

4 C B D A

Incrementing Permutations

4 1 1

slide-175
SLIDE 175

4 C D B A

Incrementing Permutations

4 1 1

slide-176
SLIDE 176

4 C D B A

Incrementing Permutations

4 1 1

slide-177
SLIDE 177

4 C D B A

Incrementing Permutations

4 1 1

slide-178
SLIDE 178

4 C D B A

Incrementing Permutations

4 2 1

slide-179
SLIDE 179

4 D C B A

Incrementing Permutations

4 2 1

slide-180
SLIDE 180

4 D C B A

Incrementing Permutations

4 2

slide-181
SLIDE 181

4 D B C A

Incrementing Permutations

4 2

slide-182
SLIDE 182

4 D B C A

Incrementing Permutations

4 2

slide-183
SLIDE 183

4 D B C A

Incrementing Permutations

4 2

slide-184
SLIDE 184

4 D B C A

Incrementing Permutations

4 2 1

slide-185
SLIDE 185

4 D C B A

Incrementing Permutations

4 2 1

slide-186
SLIDE 186

4 D C B A

Incrementing Permutations

4 2 1

slide-187
SLIDE 187

4 D C B A

Incrementing Permutations

4 2 1

slide-188
SLIDE 188

4 D C B A

Incrementing Permutations

4 2 1 1

slide-189
SLIDE 189

4 D C A B

Incrementing Permutations

4 2 1 1

slide-190
SLIDE 190

4 D C A B

Incrementing Permutations

4 1

slide-191
SLIDE 191

4 A C D B

Incrementing Permutations

4 1

slide-192
SLIDE 192

4 A C D B

Incrementing Permutations

4 1

slide-193
SLIDE 193

Incrementing Permutations

  • Find the digit to be incremented.
  • Scan backwards from the end of the sequence to find

the longest increasing sequence.

  • Increment that digit.
  • Find the smallest element bigger than the element right

before that sequence.

  • Swap those two elements.
  • Set the digits after that to zero.
  • Reverse the increasing sequence
  • Runtime: O(n) per permutation.
slide-194
SLIDE 194

Summary

  • Lehmer codes describe a permutation as a

series of elements to choose in order.

  • The factoradic number system maps directly
  • nto Lehmer codes, and thus onto

permutations.

  • By simulating what would happen if we wrote
  • ut the Lehmer code, we can derive a fast

algorithm for generating ordered permutations.

slide-195
SLIDE 195

Concluding Thoughts

slide-196
SLIDE 196

Number systems are useful in number recovery by discovering one component of the number at a time.

slide-197
SLIDE 197

Number systems are useful in enumeration by revealing structure hidden in the indices.

slide-198
SLIDE 198

Fun With Number Systems

slide-199
SLIDE 199

htiek@cs.stanford.edu

My Email Address