AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



programming-basics: write a program to generate the below series

4 6 12 18 30 42 60

programming x 165
basics x 169
Posted On : 2017-07-28 12:03:29.0
profile rkjana - anyforum.in rkjana
400
up-rate
4
down-rate

Answers


This is series of numbers where preceding and successive numbers are prime numbers.
like
3 and 5 are prime with 4 as middle number.
5 and 7 are prime.... for 6
11 and 13 are prime..for 12


public class SeriesGenerator {
public static void main(String[] args) {
int startNumber=3;
int totalElements=7;
int[] series=new int[totalElements];
int previous=0;
int i=0;
while(i<totalElements){
while(true){
if(isPrime(startNumber)){
if(previous>0 && startNumber-previous==2){
series[i]=previous+1;
i++;
}
previous=startNumber;
startNumber=startNumber+2;
break;
}
startNumber=startNumber+2;
}
}
for(int num:series){
System.out.print(num+" ");
}
}

private static boolean isPrime(int num) {
if (num < 2) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i * i <= num; i += 2)
if (num % i == 0) return false;
return true;
}

}

Posted On : 2017-07-28 20:43:48
Satisfied : 1 Yes  2 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523186220845
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in