3-17 Boolean Arrays

3-17 Boolean Arrays - next prime and repeat. (ex. See...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science 3/17/08: Boolean Arrays Making Primes.java faster: Only need to check up to the square root Eratosthenes Sieve: Start with first prime number and get rid of all its multiples in an array Go to next number not crossed off and get rid of all its multiples Make an array (letting the index be the number) and insert every cell true Then change the multiples of two to false To finish, set the prime you are on equal to “prime” and “mult” to get the next multiple…go to mult=prime+mult; change that to false. Go through each until you have crossed out all the multiples of that prime. Go to the
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: next prime and repeat. (ex. See FasterPrimes.java) To get n!: public static int fact1(int n){ int answer=1; for (int i=1; i<=n; i++){ answer=i*answer; } return answer; } Mathematical definition of n!: n! is defined as n*(n-1)! If n>1 and 1 if n=1 (this type of definition is called recursion) public static int fact2(int n){ if (n= =1) return 1; else return n*fact2(n-1); What is this doing? Fact2(10) 10*fact2(9) 9*fact2(8) 8*fact2(7) 7*fact2(6) 2*fact2(1) 1...
View Full Document

Page1 / 2

3-17 Boolean Arrays - next prime and repeat. (ex. See...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online