The Collatz Problem - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

The Collatz Problem

Description:

The input will consist of a series of pairs of integers i and j, one ... curryml_at_eddie:~$ time java Main testinput. 1 999999 525. real 0m0.312s (0m6.244s) ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 16
Provided by: matthe49
Category:
Tags: collatz | eddie | problem

less

Transcript and Presenter's Notes

Title: The Collatz Problem


1
The Collatz Problem
  • Matthew Curry
  • CS497
  • January 22nd, 2007

2
The Statement
  • The input will consist of a series of pairs of
    integers i and j, one pair of integers per line.
    All integers will be less than 1,000,000 and
    greater than 0.
  • You should process all pairs of integers and for
    each pair determine the maximum cycle length over
    all integers between and including i and j.
  • You can assume that no operation overflows a
    32-bit integer.

3
The Statement (contd)
  • For each pair of input integers i and j you
    should output i, j, and the maximum cycle length
    for integers between and including i and j.
  • These three numbers should be separated by at
    least one space with all three numbers on one
    line and with one line of output for each line of
    input.
  • The integers i and j must appear in the output in
    the same order in which they appeared in the
    input and should be followed by the maximum cycle
    length (on the same line). Emphasis added

4
Tricks
  • over all integers between and including i and
    j
  • There were no statements regarding any
    relationship between i and j (i gt j i lt j)
  • no operation overflows a 32-bit integer
  • What kind of 32-bit integer?
  • Testing yields a maximum value of 56,991,483,520

5
Correct Code
  • import java.util.Scannerclass Main static
    int collatz(long num) if (num 1) return
    1 if (num 2 1) / odd / return
    collatz(3num1) 1 else return
    collatz(num/2) 1

6
Correct Code (2)
  • public static void main(String
    args) Scanner sc new Scanner(System.in)
    int first sc.nextInt() int second
    sc.nextInt() System.out.println(first
    second ) if (first gt second)
    int tmp first first
    second second tmp

7
Correct Code (3)
  • int max 0 for (int I first i lt
    second i) int tmp collatz(i) if
    (tmp gt max) max tmp System.out.printl
    n(max)

8
But theres a problem
  • Timing data taken on a dual Opteron machine
  • curryml_at_eddie cat testinput1
    999999curryml_at_eddie time java Main lt
    testinput1 999999 525real 0m6.244suser 0m6.2
    12ssys 0m0.020s

9
Optimization Eliminating Repeating Work
10
Optimization Eliminating Repeating Work
11
Optimization Eliminating Repeating Work
12
Bigger Picture 1 to 999,999
  • In original code, the collatz method is called
    132,434,271 times.
  • Using the first optimization, collatz is called
    5,226,260 times (96 reduction).
  • Using the last optimization, collatz is called
    2,355,035 times (98 reduction).

13
Changing the Implementation
  • Add an answers integer array. This will keep,
    for any given number between 1 and 999,999, the
    answer that collatz has returned for that
    query.
  • If collatz hasnt been called, zero is stored in
    this array.
  • Declare the storage for this array in the main
    method before computation starts

14
Changing the Implementation (contd)
  • static int collatz(long num) if (num
    1) return 1 if (num lt 1000000
    answer(int)num gt 0) return answers(int)num
    int retval if (num 2 1) / odd
    / retval collatz(3num1)
    1 else retval collatz(num/2) 1 if (num
    lt 1000000) answers(int)num retval return
    retval

15
More Timing Data
  • Timing data taken on a dual Opteron machine
  • curryml_at_eddie cat testinput1
    999999curryml_at_eddie time java Main lt
    testinput1 999999 525real 0m0.312s (0m6.244s)
    user 0m0.304s (0m6.212s)sys 0m0.032s
    (0m0.020s)
Write a Comment
User Comments (0)
About PowerShow.com