Prime Factors Kata Object Mentor, Inc. fitnesse.org Copyright  2005 by Object Mentor, Inc All copies must retain this page unchanged. www.junit.org www.objectmentor.com.

Slides:



Advertisements
Similar presentations
Bowling Game Kata Object Mentor, Inc. fitnesse.org Copyright 2005 by Object Mentor, Inc All copies must retain this page unchanged.
Advertisements

Chapter 1 Writing a Program Fall Class Overview Course Information –On the web page and Blackboard –
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
Type Title Here for Tic-Tac-Toe Type names of students in group here.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
JUnit, Revisited 17-Apr-17.
Grouping Objects 1 Introduction to Collections.
Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Bowling Game Kata Object Mentor, Inc. fitnesse.org Copyright  2005 by Object Mentor, Inc All copies must retain this page unchanged.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CompSci 101 Introduction to Computer Science March 31, 2015 Prof. Rodger Thanks to Elizabeth Dowd for giving this lecture Review for exam.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Example 1 Writing Powers Write the product as a power and describe it in words. a. 44= to the second power, or 4 squared 9 to the third power,
Bart van Kuik Application Developer Oracle Corporation.
Computer Science 320 A First Program in Parallel Java.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Utilities (Part 2) Implementing static features 1.
Exception Handling How to handle the runtime errors.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Object Oriented Programming Lecture 2: BallWorld.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
More on Arrays Review of Arrays of ints, doubles, chars
Lecture 5 of Computer Science II
EECE 310: Software Engineering
Marcus Biel, Software Craftsman
Chapter 20 Generic Classes and Methods
Accessing Files in Java
Bowling Game Kata In C# with NUnit.
Bowling Game Kata Object Mentor, Inc.
null, true, and false are also reserved.
Programming in Java Lecture 11: ArrayList
AP Java Warm-up Boolean Array.
Student #7 starts with Locker 7 and changes every seventh door
Object Oriented Programming in java
JavaScript Reserved Words
Random number generators
Collections and iterators
Java Programming: Chapter 9: Recursion Second Edition
import junit. framework
Dr. Sampath Jayarathna Cal Poly Pomona
Consider the following code:
Collections and iterators
Basic Exception Handling
Presentation transcript:

Prime Factors Kata Object Mentor, Inc. fitnesse.org Copyright  2005 by Object Mentor, Inc All copies must retain this page unchanged. blog.objectmentor.com

Generating Prime Factors. Although quite short, this kata is fascinating in the way it shows how ‘if’ statements become ‘while’ statements as the number of test cases increase. It’s also a wonderful example of how algorithms sometimes become simpler as they become more general. I stumbled upon this little kata one evening when my son was in 7 th grade. He had just discovered that all numbers can be broken down into a product of primes and was interested in exploring this further. So I wrote a little ruby program, test- first, and was stunned by how the algorithm evolved. I have done this particular kata in Java 5.0. This should give you a feel for the power and convenience of some of the new features.

The Requirements. Write a class named “PrimeFactors” that has one static method: generate. –The generate method takes an integer argument and returns a List. That list contains the prime factors in numerical sequence.

Begin. Create a project named PrimeFactors Create a package named primeFactors Create a unit test named PrimeFactorsTest package primeFactors; import junit.framework.TestCase; public class PrimeFactorsTest extends TestCase { } No tests found in primeFactors.PrimeFactorsTest

The first test. package primeFactors; import junit.framework.TestCase; public class PrimeFactorsTest extends TestCase { public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); }

The first test. package primeFactors; import junit.framework.TestCase; import java.util.List; public class PrimeFactorsTest extends TestCase { public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); } private List list() { return null; }

The first test. package primeFactors; import junit.framework.TestCase; import java.util.List; public class PrimeFactorsTest extends TestCase { public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); } private List list() { return null; } package primeFactors; public class PrimeFactors { }

The first test. package primeFactors; import junit.framework.TestCase; import java.util.List; public class PrimeFactorsTest extends TestCase { public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); } private List list() { return null; } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { return new ArrayList (); } expected: but was:

The first test. package primeFactors; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); } private List list() { return new ArrayList (); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { return new ArrayList (); }

The first test. package primeFactors; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list() { return new ArrayList (); } public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { return new ArrayList (); }

The Second Test

The Second test. package primeFactors; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list() { return new ArrayList (); } public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),PrimeFactors.generate(2)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { return new ArrayList (); }

The Second test. package primeFactors; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),PrimeFactors.generate(2)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { return new ArrayList (); } varargs expected: but was:

The Second test. package primeFactors; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),PrimeFactors.generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),PrimeFactors.generate(2)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { primes.add(2); } return primes; }

The Second test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { primes.add(2); } return primes; }

The Third Test

The Third test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { primes.add(2); } return primes; } expected: but was:

The Third test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { primes.add(n); } return primes; }

The Fourth Test

The Fourth test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { primes.add(n); } return primes; } expected: but was:

The Fourth test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { if (n%2 == 0) { primes.add(2); n /= 2; } if (n > 1) primes.add(n); } return primes; }

The Fifth Test

The Fifth test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { if (n%2 == 0) { primes.add(2); n /= 2; } if (n > 1) primes.add(n); } return primes; }

The Sixth Test

The Sixth test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { if (n%2 == 0) { primes.add(2); n /= 2; } if (n > 1) primes.add(n); } return primes; } expected: but was:

The Sixth test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { while (n%2 == 0) { primes.add(2); n /= 2; } if (n > 1) primes.add(n); } return primes; } ! ! !

The Seventh Test

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { while (n%2 == 0) { primes.add(2); n /= 2; } if (n > 1) primes.add(n); } return primes; } expected: but was:

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { int candidate = 2; while (n%candidate == 0) { primes.add(candidate); n /= candidate; } if (n > 1) primes.add(n); } return primes; } expected: but was:

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); if (n > 1) { int candidate = 2; while (n % candidate == 0) { primes.add(candidate); n /= candidate; } if (n > 1) primes.add(n); return primes; } expected: but was:

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); int candidate = 2; if (n > 1) { while (n % candidate == 0) { primes.add(candidate); n /= candidate; } if (n > 1) primes.add(n); return primes; } expected: but was:

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); int candidate = 2; if (n > 1) { while (n % candidate == 0) { primes.add(candidate); n /= candidate; } if (n > 1) primes.add(n); return primes; } expected: but was:

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); int candidate = 2; while (n > 1) { while (n % candidate == 0) { primes.add(candidate); n /= candidate; } candidate++; } if (n > 1) primes.add(n); return primes; } ! ! !

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); int candidate = 2; while (n > 1) { while (n % candidate == 0) { primes.add(candidate); n /= candidate; } candidate++; } return primes; }

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); int candidate = 2; while (n > 1) { for (; n%candidate == 0; n/=candidate) primes.add(candidate); candidate++; } return primes; }

The Seventh test. package primeFactors; import static primeFactors.PrimeFactors.generate; import junit.framework.TestCase; import java.util.*; public class PrimeFactorsTest extends TestCase { private List list(int... ints) { List list = new ArrayList (); for (int i : ints) list.add(i); return list; } public void testOne() throws Exception { assertEquals(list(),generate(1)); } public void testTwo() throws Exception { assertEquals(list(2),generate(2)); } public void testThree() throws Exception { assertEquals(list(3),generate(3)); } public void testFour() throws Exception { assertEquals(list(2,2),generate(4)); } public void testSix() throws Exception { assertEquals(list(2,3),generate(6)); } public void testEight() throws Exception { assertEquals(list(2,2,2),generate(8)); } public void testNine() throws Exception { assertEquals(list(3,3),generate(9)); } package primeFactors; import java.util.*; public class PrimeFactors { public static List generate(int n) { List primes = new ArrayList (); for (int candidate = 2; n > 1; candidate++) for (; n%candidate == 0; n/=candidate) primes.add(candidate); return primes; } The algorith is three lines of code!

END