Results 1 to 3 of 3
 04122014, 12:17 PM #1Member
 Join Date
 Mar 2014
 Posts
 8
 Rep Power
 0
find the smallest prime that is greater than 2*n
Given a number n I want to find a prime number which is the smallest prime that is greater than 2*n.I have done this but it gives correct answers for a few selected inputs only.
Java Code:public int prime(int num) { int k = 0; int j; for (int i = num; i <= 2 * num; i++) { for (j = 2; j < i; j++) { if (i % j == 0) { break; } } if (j == i) k = i; } return k; }
 04122014, 02:40 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,729
 Blog Entries
 7
 Rep Power
 21
Re: find the smallest prime that is greater than 2*n
Your program doesn't match your description: if searches the numbers [num ... 2*num] and it finds the smallest prime number in that range.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 04122014, 02:45 PM #3Member
 Join Date
 Mar 2012
 Posts
 30
 Rep Power
 0
Re: find the smallest prime that is greater than 2*n
You want to find the smallest prime that is greater than 2*n. In line 4 you search a number that is between the number itself and 2 * the number. For example if you pass number 5, line 4 runs as:
Java Code:for (int i = 5; i <= 10; i++) {
Sutured
Similar Threads

how to find the smallest value from a nonempty unsorted array of integers?
By hjxlpp in forum New To JavaReplies: 10Last Post: 05172013, 02:20 PM 
Find a prime number
By bam207 in forum New To JavaReplies: 2Last Post: 11162012, 07:42 PM 
Find the Biggest Number + Smallest
By d4v1dz in forum New To JavaReplies: 19Last Post: 10262011, 07:43 AM 
find a prime number
By loja11 in forum Threads and SynchronizationReplies: 4Last Post: 02172011, 09:54 AM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 02:50 AM
Bookmarks