Please implement this method to
return a list of all prime numbers in the given range (inclusively).
A prime number is a natural number that has exactly two distinct natural number divisors, which are 1 and the prime number itself.
The first prime numbers are: 2, 3, 5, 7, 11, 13
*/
- import java.util.List;
- import java.util.ArrayList;
- public class PrimeNumber {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println(getPrimeNumbers(481,500));
- }
- public static List<Integer> getPrimeNumbers(int from, int to) {
- /*
- Please implement this method to
- return a list of all prime numbers in the given range (inclusively).
- A prime number is a natural number that has exactly two distinct natural number divisors, which are 1 and the prime number itself.
- The first prime numbers are: 2, 3, 5, 7, 11, 13
- */
- List<Integer> list = new ArrayList<Integer>();
- boolean b=false;
- for(int j=from;j<=to;j++)
- {
- b=false;
- System.out.println("from "+from);
- int i=2;
- for(;i<j/2;i++)
- {
- if(j%i==0)
- {
- b=true;
- break;
- }
- }
- System.out.println("j= "+j);
- System.out.println("b= "+b);
- if(b==false && j!=1 && j!=0)
- list.add(j);
- }
- System.out.println(list);
- return list;
- }
- }
Download Link: Click Here

0 comments:
Post a Comment