implementing another class to create the first 100 prime numbers

ok I'm a little mind blown from an assignment i have to do. We have to implement a sequence class from wiley.com/go/javaexamples (the chapter 10 example) to make a new class called PrimeSequence and it has to right align the first 100 prime sequence numbers. I don't understand the point of implementing the other class and i did it but i know im not following the assignment rules because i dont understand what im supposed to implement from the other class and i also use nothing from the other class. I'm not sure on what i have to do

Sequence Class

public interface Sequence 
{   
    int next();
}

PrimeSequence Class

public class PrimeSequence implements Sequence
{

public PrimeSequence()
{

}

public boolean isPrime(int x)
{
    for (int start = 2; start <= Math.sqrt(x); start++)
    {
        if (x % start == 0) 
        {
            return false;
        }
    }
    return true;
}

    public int next()
    {

    }
}

PrimeSequenceTester

public class PrimeSequenceTester {


public static void main(String[] args) 
{
    PrimeSequence prime = new PrimeSequence();

    int currentNumber = 2;
    int primesFound = 0;

    while (primesFound < 100) {
        if (prime.isPrime(currentNumber)) 
        {
            primesFound++;

            System.out.printf("%4s",currentNumber + " ");
            if (primesFound % 10 == 0) 
            {
                System.out.println();
            }
        }

        currentNumber++;
}
}

Answers


Here is a sample implementation using a Sieve of Eratosthenes (sieving only the odd numbers) and optimised to only sieve numbers once across multiple instances of the sequence. You need to do something a LOT simpler and just keep a track of what the current prime is and override the next() function so that when it is called you keep incrementing the value of the current prime until your isPrime() function returns true and then return that value.

import java.util.BitSet;

public class PrimeSequence implements Sequence {
    private static final BitSet OddPrimeSieve = new BitSet( 8000 );
    private static int MaxSievedPrime = 2;

    static {
        OddPrimeSieve.set(0, OddPrimeSieve.size() - 1, true );
    }

    private static int PrimeToIndex( final int prime ){
        return (prime - 3) / 2;
    }

    private static int IndexToPrime( final int index ){
        return 2*index + 3;
    }

    private static synchronized void setMaxSievedPrime( final int max ){
        MaxSievedPrime = max;
        for ( int index = PrimeToIndex( MaxSievedPrime ) + MaxSievedPrime;
                index < OddPrimeSieve.length();
                index += MaxSievedPrime )
            OddPrimeSieve.set( index, false );
    }

    int currentPrime = 2;

    @Override
    public synchronized int next() {
        final int current = currentPrime;
        if ( current == 2 )
        {
            currentPrime++;
        }
        else
        {
            if ( currentPrime > MaxSievedPrime )
                setMaxSievedPrime( currentPrime );
            currentPrime = IndexToPrime( OddPrimeSieve.nextSetBit( PrimeToIndex( currentPrime ) + 1 ) );
        }
        return current;
    }

    public static void main( final String[] args ){
        PrimeSequence p = new PrimeSequence();
        for ( int i = 0; i < 100; i++ )
            System.out.println( p.next() );
    }
}

Need Your Help

Android: startActivityForResult & setResult for a view class and an activity class

android android-intent view android-activity

I am confused and have no idea on how to use the startActivityResults and setResults to get data from previous activity. I have a view class and a activity class.