+ 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
19 ответов
+ 3
That's very confusing tho
+ 2
Send in the code as well. We can verify whether you are on the right path!
+ 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.
+ 1
ok wait
+ 1
+ 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.
+ 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
+ 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
+ 1
can you modify code acc. to you and post a link over here
+ 1
let assume 36. closest one is 31 and 37
so, d1=36-31=5 and d2=37-36=1
+ 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.
+ 1
both 3 and 5 are close to 4 by 1
+ 1
It should stop running altogether when:
(d1=1&& d2 is prime)||(d1 is prime && d2=1)
+ 1
i have to code whole things in main() , i cannot make a separate function , not separate class
+ 1
im not getting you :(
+ 1
i modifed my code. and works. But still when i write online.. it give infinity loop error or java.lang.outofmemoryerror
+ 1
:/ but 1 is not a prime number
+ 1
im confused too
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.