Question

2. Primes [2 marks] A prim e p > 1 has no factors other than 1, so p%m 0 for all m є {2.3, p-1). Test all the integers greater than 1 that you can, and identify the primes (again, write your own code). How far can you get in 10 minutes (use a clock or watch to approximately time this)? Print (or write) the last three primes that you find, and also check them using a web tool (Google prime checker). Advanced (no extra marks): you only need to check up to P, and you only need to check the primes you have already found up to vp

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// Code to find as much Prime numbers within ten minutes

import java.util.Vector;

public class PrimeIn10Minutes {
    public static void main(String[] args)
    {
        boolean flag = false;

        long startTime;
        startTime = System.currentTimeMillis();

        Vector vector = new Vector();

        long num = 2;

        while((System.currentTimeMillis() - startTime) <= 600000)
        {
            flag = checkNextPrime(num);

            if (!flag)
            {
                vector.add(num);
            }
            num++;
        }

        int vectorSize = vector.size();

        System.out.println("Last three prime numbers till ten minutes are :");
        for(int i = vectorSize-1; i > vectorSize-4; i--)
        {
            System.out.println(vector.get(i));
        }
    }

    public static boolean checkNextPrime(long num)
    {
        boolean flag = false;

        if(num/2 < 2)
            return false;

        else {
            for(long i = 2; i <= num/2; i++)
            {
                if(num % i == 0)
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }
    }
}

Output:

Last three prime numbers till ten minutes are :
1074253
1074251
1074223

Add a comment
Know the answer?
Add Answer to:
2. Primes [2 marks] A prim e p > 1 has no factors other than 1,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT