+ 2

Anyone can make a program of this ? sample input : 583 and output is 5. Below is more description

Write a java program to take an integer N.Find the closest prime number to that integer and calculate the difference between N and that closest prime number and let that difference be K.Now find the closest prime number to K and calculate the difference.Continue this process iteratively until that difference value itself is a prime or their difference is 1.The final computed value is the Total Prime Difference of N. Sample Input: 36 Sample Output: Total prime difference of 36 is 1

18th Jul 2017, 5:22 AM
Amandeep
Amandeep - avatar
19 ответов
+ 3
That's very confusing tho
18th Jul 2017, 6:19 AM
Limitless
Limitless - avatar
+ 2
Send in the code as well. We can verify whether you are on the right path!
18th Jul 2017, 5:29 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 2
What my calculations were When K=4 3-4=-1 5-4=+1 Since 3 is lesser than 4 and 5 is greater than 4 But absolute value is 1. Distance wise, both are equal. 1 each.
18th Jul 2017, 6:09 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 1
ok wait
18th Jul 2017, 5:30 AM
Amandeep
Amandeep - avatar
18th Jul 2017, 5:34 AM
Amandeep
Amandeep - avatar
+ 1
Actually it is giving correct result. But i have to run and submit it on a online compiler. Which give infinity loop error when i put if(d1==1&&d2==1) condition. But this program works correctly on solo and eclipse.
18th Jul 2017, 5:36 AM
Amandeep
Amandeep - avatar
+ 1
Im selected for a job in multi national company. they give weekly two programs. I did the other one. but im stuck in this one
18th Jul 2017, 5:37 AM
Amandeep
Amandeep - avatar
+ 1
If you are saying d1 and d2 are differences to closest prime numbers. At any time, logically speaking, both won't be equal to 1. One would be -1 and the other would be +1. (considering higher and lower primes) In case of K=4, prime numbers are 3 and 5 So, differences are -1 and +1
18th Jul 2017, 5:43 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 1
can you modify code acc. to you and post a link over here
18th Jul 2017, 5:44 AM
Amandeep
Amandeep - avatar
+ 1
let assume 36. closest one is 31 and 37 so, d1=36-31=5 and d2=37-36=1
18th Jul 2017, 5:46 AM
Amandeep
Amandeep - avatar
+ 1
and lets take 583. in loop 1: d1=583-577=6 d2=587-583=4 as 4 is not prime. start loop again. d1=4-3=1 d2=5-4=1 both are 1.
18th Jul 2017, 5:48 AM
Amandeep
Amandeep - avatar
+ 1
both 3 and 5 are close to 4 by 1
18th Jul 2017, 5:48 AM
Amandeep
Amandeep - avatar
+ 1
It should stop running altogether when: (d1=1&& d2 is prime)||(d1 is prime && d2=1)
18th Jul 2017, 5:49 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 1
i have to code whole things in main() , i cannot make a separate function , not separate class
18th Jul 2017, 5:51 AM
Amandeep
Amandeep - avatar
+ 1
im not getting you :(
18th Jul 2017, 5:53 AM
Amandeep
Amandeep - avatar
+ 1
i modifed my code. and works. But still when i write online.. it give infinity loop error or java.lang.outofmemoryerror
18th Jul 2017, 6:00 AM
Amandeep
Amandeep - avatar
+ 1
:/ but 1 is not a prime number
18th Jul 2017, 6:11 AM
Limitless
Limitless - avatar
+ 1
im confused too
18th Jul 2017, 7:55 AM
Amandeep
Amandeep - avatar
0
@limitless you're right. That's not what is asked here. The difference should either be 1 or prime - is the time when the iteration should stop.
18th Jul 2017, 6:14 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar