Prime Number Loop Help
I have used some code and written a program that asks the user for a number and it checks to see if it is prime or not. The program works fine, but I need it to loop and ask for another number or to exit. Here is my code and what my teacher has suggested to do, but I dont understand how to do this.
This program checks to see if a number is prime or not. I got a lot of help on the Java Sun Forums.
import java.io.*;
public class Prime
public static void main(String[] args)
System.out.print("Enter a number: ");
String input = readInput();
int number = Integer.parseInt(input);
int divisor = 2;
int numberbytwo = number / 2;
// Sets the factor to false uses loop to test
boolean primen =false;
while( divisor <= numberbytwo )
if (number % divisor == 0)
primen = true;
break;
divisor = divisor + 1;
if( primen )
System.out.println( number + " is NOT a prime number");
else
System.out.println( number + " is a prime number");
Input method
private static String readInput()
try
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
return keyboard.readLine();
catch (IOException e) {}
return "";
What you have done is absolutely right...If you want to keep the user in the loop after the first check, you can do it and it is quite easy. Move the prompting for a number etc..to another function just the readInput() method...call it maybe askUser() or something.....
and then in your below logic:
if( primen )
System.out.println( number + " is NOT a prime number");
// ASK THE USER IF HE/SHE wants to continue. If yes call the askUser() method....
else
System.out.println( number + " is a prime number");
It is, Jim. But not as we know it...Enlighten me 'o learned one, how should it be then??? Well, my comment suggested that the language wasn't standard,
not that it should be.
That's not a quibble: for what it's worth, I think fully spelt out words are
probably a good thing in an international forum with many members for
whom English is not their first language. And that care and
attention to language is important both for describing problems and
their solutions, and for coming up with something acceptable to the
compiler. But more important than either of these, is the need to
communicate, and to help.
You were doing both of these. And I wasn't. So I shouldn't have
stomped on your thread. For that I am sorry.
(But destin started it! And it's "Enlighten me, oh learned one; how, then,
should it be?")
Similar Messages
-
[Novice Help]what's wrong of my prime number programme !!~?
import javax.swing.*;
public class prime1 {
public static void main (String args[])
String number;
int n;
number = JOptionPane.showInputDialog( "Enter" );
n = Integer.parseInt( number );
for ( int counter = 2 ; counter < n ; counter++ ){
if ( n % counter != 0 )
System.out.println(
" It is a prime number ");
else
System.out.println(
" It is not a prime number ");
System.exit(0);
Why n only mod 2,but not mod all number which is smaller then n?
when i input 9 ,it is wrong ~
i discover that when 9 is mod 2 ,it will not continue to mod 3,4,5,6,7,8The else only applied to the lien directly after it as there were no brackets to collect the two statements in an else block.
This means that the program was going into the for loop, checking you if/else then always running System.exit(0) which stopped the program before the loop had finnished.
Your statement "it is prime" is also in the wrong place. This really should say "it is no divisible by " + counter
as there are still numbers to check.
Try this revised version and see if you can understand the difference:
import javax.swing.*;
public class prime1 {
public static void main (String args[]) {
String number;
int n;
number = JOptionPane.showInputDialog( "Enter" );
n = Integer.parseInt( number );
for ( int counter = 2 ; counter < n ; counter++ ){
if ( n % counter != 0 )
System.out.println("It is not divisible by " + counter);
else{
System.out.println("It is not a prime number");
System.exit(0);
System.out.println("It is prime"); -
Who can help me write this Java code show the prime number ???? PLEASEEEEEE
Write java code print prime number like this
Sample Screen Print:
Initial matrix with N = 37
2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37
Intermediate results (after 1st iteration)
2 3 5 7 9
11 13 15 17 19
21 23 25 27 29
31 33 35 37
Intermediate results (after 2nd iteration)
2 3 5 7
11 13 17 19
23 25 29
31 35 37
Intermediate results (after 7th iteration)
2 3 5 7
11 13 17 19
23 29
31 37
Final results
2 3 5 7 11 13 17 19 23 29
31 37
How to write this code ?
Please Help me!
Thank you so muchhh ?????h2. {color:#ff0000}Multiplepost{color}
Replies here: http://forum.java.sun.com/thread.jspa?threadID=5241012&tstart=0
There is a useful answer the original thread. Answer it, or ignore it as you like, but don't create multiple threads. -
Array size limitations... and prime number programs.
What are they? I am an ameteur programmer who wrote an extremely efficient prime finding program, if i do say so myself. The program doesnt find high primes, just prime numbers from 1-1,000,000,000 so far. Thats one version, the next version i use is faster, but because it uses an ArrayList, it can only do the primes 1-10,000,000. I am trying to work the programs up to primes with 50 or more digits, but ArrayList runs out of space, Integer is too small, I dont quite know how to switch all my code to be compatable with BigIntegers, and I cannot find a limit to arrays. I guess that I could find big primes if a) Integer was bigger, b)I tried, but what i wanted to do, because the second program is way more effecient, is to use an array to store primes instead of an ArrayList. The only problem? Arrays cannot be appended, so I need to know the limit in size for an array... So far, from my tests, I have found the limit to be somewhere around, 15,380,277. The only problem with this is that every time i compile, i can use a different number. So I would like it if a) somone could tell me the limit to an array's size, b) ideas for programs that can find primes with 50 or more digits, c) Down below is the code, could someone tell me how to convert it to BigIntegers?
private void primeFinder1root(int beg, int end){
int tmp = 0;
int counter = 0;
int counter2 = 2;
boolean flag;
for(int n = 3; n < end; n+=2){
if(n%5!=0){
flag = true;
for(int d = 3; d <= Math.sqrt(n) && flag; d+=2){
counter++;
if(n%d == 0)
flag = false;
if(flag){
System.out.println(n);
counter2++;
tmp = n;
if(counter2%100000 == 0)
System.out.println(n);
System.out.println();
System.out.println("The program looped " + counter + " times. There were " + counter2 + " primes found. "
+ tmp + " was the last prime found.");
}That is the first progam that does not use an ArrayList, but is still extremely effecient, it only looped 1,744,118,556 times to find the primes 1-100,000,000 which seems like a lot, but it truely isn't. I realize that by using counters and printing, I am slowing the program down immensly, but i am fine with that.
public void primeFinder(){
boolean flag;
int tmp = 0;
int tmp2 = 0;
int counter = 0;
primes.add(2);
for(int n = 3; n < end; n+=2){
if(n%5!=0){
flag = true;
for(int i = 0; i < primes.size()/2 && ((Integer)primes.get(i)).intValue() <= Math.sqrt(n) && flag; i++){
tmp = ((Integer)primes.get(i)).intValue();
if(n%tmp == 0)
flag = false;
counter ++;
if(flag && n!=1){
System.out.println(n);
primes.add(n);
tmp2 = n;
if(primes.size() % 100000 == 0)
System.out.println(n);
primes.add(0, 1);
System.out.println(counter + " " + primes.size() + " " + tmp2);
}This is the code that stores all the primes it finds in an ArrayList, and then compares all numbers to the ArrayList of primes. This is extremely more effecient than the first, looping only 278,097, 308 times to find the primes 1-10,000,000 (the other looped 868,772,491 times). I used 10,000,000 as my example because this program cannot go to 100,000,000 because there are 5,761,455 primes and the ArrayList can only hold ~4,000,000 objects. Because of this ~4,000,000 object limitation on the ArrayList I have set my sites on the Array. This is the reason why I want to know the limitations of an Array if you could please tell me. If you could also, I would like help making my code compatable with BigIntegers as well.
Well, sorry for the very long post, but thank you if you answer it and took the time to read it.
Dumber_ChildI too was in the quest to develop the most efficient prime number code few years ago when I was a student.
Here is a more fine tuned version for your code
public static long findPrimes(int max, ArrayList out){
long count=0;
FastList primes = new FastList((int)Math.sqrt(max));
primes.add(2);
for (int i=3; i<=max; i+=2)
int al_size = primes.size();
double sqrt = Math.sqrt(i);
boolean prime_flag =true;
boolean loop_flag = prime_flag;
for (int j=0; j<al_size && loop_flag; j++)
int val = primes.get(j);
if (i%val == 0)
loop_flag = prime_flag = false;
else if (val > sqrt)
loop_flag = false;
count++;
if (prime_flag)
primes.add(i);
primes.addToArrayList(out);
return count;
Following a data structure to store the prime numbers while processing
Since this holds first number of primes in an array instead of in an ArrayList
the get will work much faster and for those elements the casting is not required
static class FastList
ArrayList list = new ArrayList();
int cache[];
int pointer = 0;
int fastAreaSize;
public FastList(int fastAreaSize){
cache = new int[fastAreaSize];
this.fastAreaSize = fastAreaSize;
public void add(int i){
if (pointer < fastAreaSize)
cache[pointer] = i;
list.add(new Integer(i));
pointer++;
public int size(){
return pointer;
public int get(int i){
if (i<fastAreaSize)
return cache;
else
return((Integer)list.get(i)).intValue();
public void addToArrayList(ArrayList al){
for (int i=0; i<pointer; i++)
if (i<fastAreaSize)
al.add(new Integer(cache[i]));
else
al.add(list.get(i));
When running in my pc
above code detected primes within range 0-10000000 in 281809517 iterations within 6.718secs
while your original code did the same in 278097308 iterations within 13.687 secs.
By the way i removed the check for '5' thats why my code does more iterations.
Notice that I have relocated code like Math.sqrt and ((Integer).....).intValue() to reduce the re calculating the same thing. That saved a lot of time. -
I'm trying to create a simple program that tells me if the number entered is a prime number or not. I'm kind of stuck... I think it the problem is more with the math than the actual program itself. This is what I got:
import java.util.*;
public class Ch5Prg7
static Scanner console = new Scanner(System.in);
public static void main (String[]args)
int num;
System.out.println("Enter a positive integer.");
num = console.nextInt();
if (num == 1){System.out.println("That number is not prime.");}
if (num == 2){System.out.println("That number is prime.");}
if (num > 2){
// not sure what to do here
}Yeah, I know. It's not much. I just don't really know where to begin. Any help with the math or anything in general would be greatly appreciated. :)i believe the most efficient algorithm is to check every number less than or equal to the number in question, starting at 2. that is, for the number 9, try numbers up to 3 starting at 2. this could be implemented as following:
boolean isPrime = false;
if( num > 1 ) { //if this block is skipped, isPrime == false
int root = Math.sqrt( num ); //store result to avoid calling every time through loop (every iteration)
for( int divisor = 2; divisor <= root; divisor++ ) {
if( num % divisor == 0 ) {
isPrime = false; //number is NOT prime
break; //no need to continue loop
if( isPrime ) {
System.out.println("That number is prime.");
else {
System.out.println("That number is not prime.");
}Another algorithm you could use, although it may be slower due to the allocation of the array would be:
boolean isPrime = false;
if( num > 1 ) { //if this block is skipped, isPrime == false
int root = (int)Math.sqrt( num ); //store result to avoid calling every time through loop (every iteration)
bool[] possibleFactors = new int[ root + 1 ];
//all array elements are initialized to false by the Java VM
//false indicates untested value, true indicates tested value
for( int divisor = 2; divisor <= root; divisor++ ) {
if( possibleFactors[ divisor ] == false ) {
if( num % divisor == 0 ) {
isPrime = false; //number is NOT prime
break; //no need to continue loop
else {
//set all multiples of divisor to true (indicating they were tested) since
//none of the multiples will divide evenly if one of there factors didn't
for( int i = divisor; i <= root; i += divisor ) {
possibleFactors[ i ] = true;
if( isPrime ) {
System.out.println("That number is prime.");
else {
System.out.println("That number is not prime.");
} -
Prime Number Formula in PL/SQL
Hi Friends,
Oracle 11.2.0.1
Windows XP Prof.
Two questions, but almost same nature :
1.Is there any formula by which we can get the total count of prime numbers for a given number; i mean suppose I says 10, it means it should return 3; i.e. there are total 3 prime number between 1-10.
Here I can use a function which will say me that a given number is prime or not, and i will loop up to that given number. But, this is not actually a Formula, its kind of calculation. I am just looking a fixed matehematical formula for it; if it exists or not. If exists, then how that PL/SQL block will looks like.
2.Suppose I wish to get 50 prime numbers (starting from 1), then what will be the 50th prime number, i mean upto which highest number my calculation will expanded.
If possible, this should also be based upon a fixed formula.
Not actually a business requirement, but my nephew (student of class 9th) asked me these questions. I said him, neither I am student of maths nor that much knowledgeable in PL/SQL.
I asked him, why you are looking these formulas. He said, just these questions raised in my mind and curious to know that is there a fixed formula in maths exists or not. I said him, "Have you asked from your Maths teacher?" He said "Yes, sir told me, that there is no such a fixed mathematical formula, because there is no fixed gap in their ranges, what you says?"
I thought, let me write here.
Regards
Girish SharmaUsing SQL & MODEL. Not the fastest solution, but calculates first 1000 prime numbers in 5 seconds:
VARIABLE cnt NUMBER
EXEC :cnt := 1000;
SELECT prime
FROM dual
CONNECT BY LEVEL < 3
MODEL
DIMENSION BY(
level i
MEASURES(
level prime,
0 probe,
0 is_prime,
2 prime_cnt
RULES ITERATE(1e9) UNTIL(prime_cnt[1] >= :cnt)
probe[any] = 2 * iteration_number + 3,
is_prime[1] = case
when min(mod(probe,prime))[i between 2 and probe[1] / 2] = 0
then 0
else 1
end,
prime_cnt[1] = prime_cnt[1] + is_prime[1],
probe[1] = case
when is_prime[1] = 0
then 1
else probe[1]
end,
prime[probe[1]] = probe[1]
PRIME
1
2
3
5
7
11
13
17
19
23
29
PRIME
31
37
41
43
47
53
59
61
67
71
73
PRIME
79
83
89
97
101
103
107
109
113
127
131
PRIME
137
139
149
151
157
163
167
173
179
181
191
PRIME
193
197
199
211
223
227
229
233
239
241
251
PRIME
257
263
269
271
277
281
283
293
307
311
313
PRIME
317
331
337
347
349
353
359
367
373
379
383
PRIME
389
397
401
409
419
421
431
433
439
443
449
PRIME
457
461
463
467
479
487
491
499
503
509
521
PRIME
523
541
547
557
563
569
571
577
587
593
599
PRIME
601
607
613
617
619
631
641
643
647
653
659
PRIME
661
673
677
683
691
701
709
719
727
733
739
PRIME
743
751
757
761
769
773
787
797
809
811
821
PRIME
823
827
829
839
853
857
859
863
877
881
883
PRIME
887
907
911
919
929
937
941
947
953
967
971
PRIME
977
983
991
997
1009
1013
1019
1021
1031
1033
1039
PRIME
1049
1051
1061
1063
1069
1087
1091
1093
1097
1103
1109
PRIME
1117
1123
1129
1151
1153
1163
1171
1181
1187
1193
1201
PRIME
1213
1217
1223
1229
1231
1237
1249
1259
1277
1279
1283
PRIME
1289
1291
1297
1301
1303
1307
1319
1321
1327
1361
1367
PRIME
1373
1381
1399
1409
1423
1427
1429
1433
1439
1447
1451
PRIME
1453
1459
1471
1481
1483
1487
1489
1493
1499
1511
1523
PRIME
1531
1543
1549
1553
1559
1567
1571
1579
1583
1597
1601
PRIME
1607
1609
1613
1619
1621
1627
1637
1657
1663
1667
1669
PRIME
1693
1697
1699
1709
1721
1723
1733
1741
1747
1753
1759
PRIME
1777
1783
1787
1789
1801
1811
1823
1831
1847
1861
1867
PRIME
1871
1873
1877
1879
1889
1901
1907
1913
1931
1933
1949
PRIME
1951
1973
1979
1987
1993
1997
1999
2003
2011
2017
2027
PRIME
2029
2039
2053
2063
2069
2081
2083
2087
2089
2099
2111
PRIME
2113
2129
2131
2137
2141
2143
2153
2161
2179
2203
2207
PRIME
2213
2221
2237
2239
2243
2251
2267
2269
2273
2281
2287
PRIME
2293
2297
2309
2311
2333
2339
2341
2347
2351
2357
2371
PRIME
2377
2381
2383
2389
2393
2399
2411
2417
2423
2437
2441
PRIME
2447
2459
2467
2473
2477
2503
2521
2531
2539
2543
2549
PRIME
2551
2557
2579
2591
2593
2609
2617
2621
2633
2647
2657
PRIME
2659
2663
2671
2677
2683
2687
2689
2693
2699
2707
2711
PRIME
2713
2719
2729
2731
2741
2749
2753
2767
2777
2789
2791
PRIME
2797
2801
2803
2819
2833
2837
2843
2851
2857
2861
2879
PRIME
2887
2897
2903
2909
2917
2927
2939
2953
2957
2963
2969
PRIME
2971
2999
3001
3011
3019
3023
3037
3041
3049
3061
3067
PRIME
3079
3083
3089
3109
3119
3121
3137
3163
3167
3169
3181
PRIME
3187
3191
3203
3209
3217
3221
3229
3251
3253
3257
3259
PRIME
3271
3299
3301
3307
3313
3319
3323
3329
3331
3343
3347
PRIME
3359
3361
3371
3373
3389
3391
3407
3413
3433
3449
3457
PRIME
3461
3463
3467
3469
3491
3499
3511
3517
3527
3529
3533
PRIME
3539
3541
3547
3557
3559
3571
3581
3583
3593
3607
3613
PRIME
3617
3623
3631
3637
3643
3659
3671
3673
3677
3691
3697
PRIME
3701
3709
3719
3727
3733
3739
3761
3767
3769
3779
3793
PRIME
3797
3803
3821
3823
3833
3847
3851
3853
3863
3877
3881
PRIME
3889
3907
3911
3917
3919
3923
3929
3931
3943
3947
3967
PRIME
3989
4001
4003
4007
4013
4019
4021
4027
4049
4051
4057
PRIME
4073
4079
4091
4093
4099
4111
4127
4129
4133
4139
4153
PRIME
4157
4159
4177
4201
4211
4217
4219
4229
4231
4241
4243
PRIME
4253
4259
4261
4271
4273
4283
4289
4297
4327
4337
4339
PRIME
4349
4357
4363
4373
4391
4397
4409
4421
4423
4441
4447
PRIME
4451
4457
4463
4481
4483
4493
4507
4513
4517
4519
4523
PRIME
4547
4549
4561
4567
4583
4591
4597
4603
4621
4637
4639
PRIME
4643
4649
4651
4657
4663
4673
4679
4691
4703
4721
4723
PRIME
4729
4733
4751
4759
4783
4787
4789
4793
4799
4801
4813
PRIME
4817
4831
4861
4871
4877
4889
4903
4909
4919
4931
4933
PRIME
4937
4943
4951
4957
4967
4969
4973
4987
4993
4999
5003
PRIME
5009
5011
5021
5023
5039
5051
5059
5077
5081
5087
5099
PRIME
5101
5107
5113
5119
5147
5153
5167
5171
5179
5189
5197
PRIME
5209
5227
5231
5233
5237
5261
5273
5279
5281
5297
5303
PRIME
5309
5323
5333
5347
5351
5381
5387
5393
5399
5407
5413
PRIME
5417
5419
5431
5437
5441
5443
5449
5471
5477
5479
5483
PRIME
5501
5503
5507
5519
5521
5527
5531
5557
5563
5569
5573
PRIME
5581
5591
5623
5639
5641
5647
5651
5653
5657
5659
5669
PRIME
5683
5689
5693
5701
5711
5717
5737
5741
5743
5749
5779
PRIME
5783
5791
5801
5807
5813
5821
5827
5839
5843
5849
5851
PRIME
5857
5861
5867
5869
5879
5881
5897
5903
5923
5927
5939
PRIME
5953
5981
5987
6007
6011
6029
6037
6043
6047
6053
6067
PRIME
6073
6079
6089
6091
6101
6113
6121
6131
6133
6143
6151
PRIME
6163
6173
6197
6199
6203
6211
6217
6221
6229
6247
6257
PRIME
6263
6269
6271
6277
6287
6299
6301
6311
6317
6323
6329
PRIME
6337
6343
6353
6359
6361
6367
6373
6379
6389
6397
6421
PRIME
6427
6449
6451
6469
6473
6481
6491
6521
6529
6547
6551
PRIME
6553
6563
6569
6571
6577
6581
6599
6607
6619
6637
6653
PRIME
6659
6661
6673
6679
6689
6691
6701
6703
6709
6719
6733
PRIME
6737
6761
6763
6779
6781
6791
6793
6803
6823
6827
6829
PRIME
6833
6841
6857
6863
6869
6871
6883
6899
6907
6911
6917
PRIME
6947
6949
6959
6961
6967
6971
6977
6983
6991
6997
7001
PRIME
7013
7019
7027
7039
7043
7057
7069
7079
7103
7109
7121
PRIME
7127
7129
7151
7159
7177
7187
7193
7207
7211
7213
7219
PRIME
7229
7237
7243
7247
7253
7283
7297
7307
7309
7321
7331
PRIME
7333
7349
7351
7369
7393
7411
7417
7433
7451
7457
7459
PRIME
7477
7481
7487
7489
7499
7507
7517
7523
7529
7537
7541
PRIME
7547
7549
7559
7561
7573
7577
7583
7589
7591
7603
7607
PRIME
7621
7639
7643
7649
7669
7673
7681
7687
7691
7699
7703
PRIME
7717
7723
7727
7741
7753
7757
7759
7789
7793
7817
7823
PRIME
7829
7841
7853
7867
7873
7877
7879
7883
7901
7907
1000 rows selected.
Elapsed: 00:00:05.07
SQL>
SY. -
Hello,
I'm a high school student and am making a program which calculates the next prime number greater than the inputed number. The rules are that you cannot use any other classes other than those that are imported, but I think you can use any class in lang (you can definitely use the math class).
The program determines how fast it took to find the prime number. Entering the number 9*10^16 (9 with sixteen 0s), my PC calculated it in about 22534 milliseconds (22 seconds). I'm trying to improve this since I've seen people do at about 7 seconds. Any ideas on how to make this program faster, about 15 seconds faster?
Here is my code for the best i could come up with:
import javax.swing.JOptionPane;
import javax.swing.JApplet;
import javax.swing.JTextArea;
import java.awt.Container;
public class PrimeFinder extends JApplet
public void init()
String ms = JOptionPane.showInputDialog("Please enter a number:");
long b = Long.parseLong(ms);
long base = b;
long num = 3;
long start = System.currentTimeMillis();
while(num<=Math.sqrt(base))
if(base%2==0 || base%num==0)
base++;
num=3;
else num+=2;
long time = System.currentTimeMillis() - start;
String result = "Prime Finder:\n\nBase Number:\t" + b + "\nNext Prime:\t" + base + "\nTime:\t" + time;
JTextArea y = new JTextArea();
y.setText(result);
Container container = getContentPane();
container.add(y);
}PS: please run my code on your PC to check how fast your PC does it before writing new code since a better PC will obviously do it faster with the same code.This is what I am using:
public static void main(String[] args)
String ms = JOptionPane.showInputDialog("Please enter a number:");
long b = Long.parseLong(ms);
long base = b;
double sqrt = Math.sqrt(base);
System.out.println("Best Before: 19469");
long start = System.currentTimeMillis();
for(long num = 3; num <= sqrt; num += 2)
if(base%num==0)
base++;
num = 3;
sqrt = Math.sqrt(base);
else
num += 2;
long time = System.currentTimeMillis() - start;
String result = "Prime Finder:\n\nBase Number:\t" + b + "\nNext Prime:\t" + base + "\nTime:\t" + time;
System.out.println(result);
}I see a very significant time change between the while loop and for loop. -
Need to hold very large number - please help
Hello all,
I am working with programming the SSH handshake and need to represent a very large number in order to get it to work correctly. The number is:
179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007
which is: 1.7 *10^318
and we know the largest double is:
1.7976931348623157E308
Can someone please help me with representing this number? It is a very large prime number that is used in the key exchange for SSH.
Thank you all for you time
MaxAnd who's the slowest old sod again?
This is amazing: I read a new topic, no repliesyet,
I check it again, nothing yet,
I craft my reply and presto: some quick fingersbeat
me to it again ...Grolsch makes slow :P ;)It's still only 11:45ag ... and I'm still waiting ;-)
Jos -
Finding the next smaller and next larger prime number
Hey all, will this code help me find the next larger and next smaller prime number? I only ask because my main() isnt outputting anything, but everything looks fine there. So assuming I may have done something that didnt make sense here.
//returns the next larger prime number
long largerGetter = num;
long lG = num + 1;
while(largerGetter != 1)
largerGetter = PrimeChecker.primeOrNot(lG);
lG++;
nextLargerPrimeNumber = lG;
//returns next smaller prime
long smallerGetter = num;
long sG = num - 1;
while(smallerGetter != 1)
smallerGetter = PrimeChecker.primeOrNot(sG);
sG--;
nextSmallerPrimeNumber = sG;JimmyV88 wrote:
Hey all, will this code help me find the next larger and next smaller prime number? ...If this isn't homework (which it probably is), use the BigInteger class to find a [next probable prime|http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html#nextProbablePrime()] given a prime number or keep subtracting two from that given prime and check if that number is [(probable) prime|http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html#isProbablePrime(int)] to find the previous prime. -
(V7.X) PRIME NUMBER 를 생성해 주는 FUNCTION
제품 : SQL*PLUS
작성날짜 : 1998-09-22
(V7.X) PRIME NUMBER 를 생성해 주는 FUNCTION
다음은 input value x 보다 크면서, 가장 작은 prime number 를 return 해 주는 function 입니다.
create or replace function lprime (x in number) return number is
w number;
i number;
begin
w := x;
if w <= 2
then
w :=3;
return w;
end if;
if mod(w,2) = 0
then
w := w +1;
end if;
loop
i:=3;
loop
if (i*i) > w
then
return w;
end if;
if mod(w,i) = 0
then
exit;
end if;
i := i + 2;
end loop;
w := w + 2;
end loop;
end;
실행의 예
SQL> select lprime(10000) from dual;
LPRIME(10000)
10007 -
Hi.I need help.my iphone was Stolen .how can I get back my iphone. Serial No.DX******PMW.could you help me find my iphone.thank you very much.could you send me the ICCID number to help me find the iphone.if you can help me.
<Personal Information Edited by Host>
Sincerely a Chinese girl really need your helpSorry we are all users on this User Community No Apple Staff
Read this .
http://support.apple.com/kb/HT2526
Apple do not and cannot assist in finding stolen property ,that is the responsility of your Police -
hi.. i forgot my apple id and password.. i cant activate my ipod touch..
but here's the serial number, *************** please help me.. thanks
<Edited by Host>We are fellow users here on these forums, you're not talking to iTunes Support nor Apple - I've asked the hosts to remove your iPod's serial number address from your post.
Only you can activate your iPod by typing in your id and password, or you can remove a device from your account via http://icloud.com . If you can't remember your account id then do you have any of its downloads on your computer's iTunes : Recovering a forgotten iTunes Store account name ? If not then you could try http://iforgot.apple.com . You can also reset your password via the 'iforgot' site, or you can contact Support in your country and they should be able to reset your password (as long as you can prove your id and that it's your account) : Apple ID: Contacting Apple for help with Apple ID account security
iOS 7 and activation lock : iCloud: Find My iPhone Activation Lock in iOS 7
What if I forget my Apple ID password?
If you forget your password, you can reset it at My Apple ID (appleid.apple.com) or by contacting Apple Support and verifying your identity. Once your password has been reset, it will work normally with Find My iPhone and Activation Lock.
If you forget your password and cannot reset it, you will lose access to your Apple ID and may be unable to use or reactivate your device. To help prevent this, visit My Apple ID periodically to review and update your account information. -
i want to original owner tel: number plz help me becouse my iphone is lock
Welcome to the user to User Technical Support Forum provided by Apple.
Syazuan wrote:
i want to original owner tel: number ...
No one here can assist you with that.
The Apple ID and Password that was Originally used to Activate the iDevice is required
If you do not have that information you will not be able to use the Device.
Activation Lock in iOS 7 > http://support.apple.com/kb/HT5818 -
Is this the best way to test a prime number?
Ok so I think I finally got my program to work. I tested It with very big 8 digit prime numbers if that means anything. here are 2 Questions about my program"
-Is this a way of finding prime numbers? (does the program fulfill its purpose?)
and Second is there a smarter way of doing it?
Here is the program:
//This program tests if a number is a prime number
import java.util.Scanner;
public class Main
public static void main ( String args [ ] )
Scanner input = new Scanner( System.in );
int number;
int bob;
System.out.print(" Enter the number you wish to test:\n");
number = input.nextInt();
if (number == 1)
System.out.print(" 1 is divisible only by 1, therefore it is not prime or composite\n");
if (number == 2)
System.out.print(" It's Prime\n ");
for ( int test = 2 ; test < number ; test++ )
bob = number % test;
if ( bob == 0)
System.out.print("It's Composite\n");
return;
System.out.println("It's Prime");
}I got interested in this ...
//1. PrimeTester_BruteForce: found 3001134 primes <= 50000000 in 410500 milliseconds
//2. PrimeTester_SieveOfEratosthenes: found 3001134 primes <= 50000000 in 4422 milliseconds
//3. PrimeTester_SieveOfAtkin: found 3001134 primes <= 50000000 in 24843 milliseconds
... so Wkipedia's SieveOfAtkin algorithm apparently requires some serious optimization before it outruns the Greeks... see http://cr.yp.to/primegen.html for an optimized C implementation (which I can't follow).
PrimeTester_BruteForce.java//find all prime numbers upto the given maximum.
//Using the brute force method
//http://www.newton.dep.anl.gov/newton/askasci/1995/math/MATH039.HTM
//PrimeTester_BruteForce: found 3001134 primes <= 50000000 in 410500 milliseconds
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class PrimeTester_BruteForce {
public static void main(String[] argv) throws IOException {
long start = System.currentTimeMillis();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("PrimeTester_BruteForce.txt")));
int limit = 50000000;
int count = 0;
nextn: for (int n=2; n<=limit; n++) {
int top = (int)Math.sqrt(n);
for(int i=2; i<=top; i++) {
if(n%i==0) {
continue nextn;
out.print(n);
out.print(" ");
if(++count%20==0) out.println();
long took = System.currentTimeMillis()-start;
out.println();
out.println("PrimeTester_BruteForce: found "+count+" primes <= "+limit+" in "+took+" milliseconds");
out.close();
PrimeTester_SieveOfEratosthenes.java/******************************************************************************
//find all prime numbers upto the given maximum.
//http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
limit = 1000000; // arbitrary search limit
// pressume all are prime
for (i=2; i<=limit; i++) is_prime[i] = true;
// eliminate multiples of each prime, starting with its square
for (n=2; n<=sqrt(limit); n++) {
if (is_prime[n]) {
for (i=n^2; i<=limit; i+=n) is_prime[i] = false;
// print the results
for (n=2; n=<limit; n++) {
if (is_prime[n]) print(n);
//PrimeTester_SieveOfEratosthenes: found 164185 primes <= 1000000 in 125 milliseconds
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class PrimeTester_SieveOfEratosthenes {
public static void main(String[] argv) throws IOException {
long start = System.currentTimeMillis();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("PrimeTester_SieveOfEratosthenes.txt")));
int limit = 50000000;
int n, i;
boolean[] is_prime = new boolean[limit+1];
// pressume all are prime
is_prime[2] = true;
for (i=2; i<=limit; i++) {
is_prime[i] = true;
// eliminate multiples of each prime, starting with its square
for (n=1; n<=Math.sqrt(limit); n++) {
if (is_prime[n]) {
for (i=n*2; i<=limit; i+=n) {
is_prime[i] = false;
// print the results
int count = 0;
for (n=2; n<=limit; n++) {
if (is_prime[n]) {
out.print(n);
out.print(" ");
if(++count%20==0) out.println();
long took = System.currentTimeMillis()-start;
out.println();
out.println("PrimeTester_SieveOfEratosthenes: found "+count+" primes <= "+limit+" in "+took+" milliseconds");
out.close();
PrimeTester_SieveOfAtkin.java/******************************************************************************
//find all prime numbers upto the given maximum.
//http://en.wikipedia.org/wiki/Sieve_of_Atkin
limit = 1000000; // Arbitrary search limit
array is_prime[5:limit] initial false; // Sieve array.
biginteger x,y,n,k; // Must be able to hold 5*limit: 4x^2 + y^2!
// put in candidate primes: integers which have an odd number of representations
// by certain quadratic forms.
for x:=1:sqrt(limit) do
for y:=1:sqrt(limit) do
n:=4x^2 + y^2; if n<=limit and n%12 = 1 or 5 then is_prime[n]:=not is_prime[n];
n:=3x^2 + y^2; if n<=limit and n%12 = 7 then is_prime[n]:=not is_prime[n];
n:=3x^2 - y^2; if n<=limit and x>y and n%12 = 11 then is_prime[n]:=not is_prime[n];
next y;
next x;
// eliminate composites by sieving
// if n is prime, omit all multiples of its square; this is sufficient because
// composites which managed to get on the list cannot be square-free
for n:=5:sqrt(limit) do
if is_prime[n] then for k:=n^2:limit:n^2 do is_prime[k]:=false; next k;
next n;
// Present the results.
print 2, 3; for n:= 5:limit do if is_prime[n] then print n; next n;
//PrimeTester_SieveOfAtkin: found 441 primes <= 1000000 in 203 milliseconds
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class PrimeTester_SieveOfAtkin {
public static void main(String[] argv) throws IOException {
long start = System.currentTimeMillis();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("PrimeTester_SieveOfAtkin.txt")));
int limit = 50000000;
int sqrt = (int)Math.sqrt(limit);
boolean[] is_prime = new boolean[limit+1]; // Sieve array.
int x, y, n, k; // Must be able to hold 5*limit: 4x^2 + y^2!
// put in candidate primes: integers which have an odd number of representations by certain quadratic forms.
for (x=1; x<=sqrt; x++) {
for (y=1; y<=sqrt; y++) {
//n=(4*(x^2)) + y^2; if (n<=limit && n%12 in(1,5)) is_prime[n] = !is_prime[n];
int xSq = (int)Math.pow(x,2);
int ySq = (int)Math.pow(y,2);
n = 4*xSq + ySq;
if (n<=limit && (n%12==1||n%12==5)) {
is_prime[n]= !is_prime[n];
//debug("A: x="+x+", y="+y+", is_prime["+n+"]="+is_prime[n]);
//n=((3*(x^2)) + y^2; if (n<=limit && n%12==7) is_prime[n] = !is_prime[n];
n = 3*xSq + ySq;
if (n<=limit && n%12==7) {
is_prime[n]= !is_prime[n];
//debug("B: x="+x+", y="+y+", is_prime["+n+"]="+is_prime[n]);
//debug(" if (n<=limit:"+limit+" && n%12:"+(n%12)+"==7");
//debug(" (3*(x^2)):"+xSq+" + (y^2):"+ySq);
//n=((3*(x^2)) - y^2; if (n<=limit && x>y && n%12==11) is_prime[n]= !is_prime[n];
n = 3*xSq - ySq;
if (n<=limit && x>y && (n%12==11)) {
is_prime[n]= !is_prime[n];
//debug("C: x="+x+", y="+y+", is_prime["+n+"]="+is_prime[n]);
}//next y
}//next x
// eliminate composites by sieving
// if n is prime, omit all multiples of its square; this is sufficient because
// composites which managed to get on the list cannot be square-free
// for (n:=5:sqrt(limit)) {
// if (is_prime[n]) for (k:=n^2:limit:n^2) is_prime[k]:=false;
for (n=5; n<=sqrt; n++) {
if (is_prime[n]) {
int nSq = (int)Math.pow(n,2);
for(k=nSq; k<=limit; k+=nSq) {
is_prime[k]=false;
//debug("D: n="+n+" is_prime["+k+"]="+is_prime[k]);
// Present the results.
int count = 2;
out.print("2 3 ");
for(n=5; n<=limit; n++) {
if(is_prime[n]) {
out.format("%d ", n);
if(++count%20==0) out.println();
long took = System.currentTimeMillis()-start;
out.println();
out.println("PrimeTester_SieveOfAtkin: found "+count+" primes <= "+limit+" in "+took+" milliseconds");
out.close();
//private static void debug(String msg) {
// System.out.println(msg);
} -
I bought a new Mac Pro and migrated my FCP from my old one. But the new old won't accept my product serial number. Help!
Was the FCS on your old machine an upgrade? You will need to enter your original, full version serial first, and then you will be prompted for the upgrade serial number.
Hope this helps.
MtD
Maybe you are looking for
-
I want to download all the songs from iTunes Match back to my PC (iTunes). Instead of downloading one by one, can I have all the songs downloaded back to my iTunes by one shot (while I sleap)?
-
How can I change the email address on my account?
My account is tied to an old email address which hasn't existed for about a decade, how do I change this?
-
Hi. I create a simple page & put an apDiv box on the page. I then put a small swf file in it. Everything looks good & I even test it out in "Preview in Expolorer & Firefox". Video plays great. Only thing is when I go to the live URL nothing apears...
-
i am using hsqldb1.8.0 and trying to access the standlone database thru hibernate.. my hibernate configuration file contains <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN
-
Using DB Connection with DBPool
Hi, We are about to develop some Webdynpro applications and we have a requirement of accesing R/3 and a DB in the same Webdynpro Component, I understand you can not use the DB as a model but maybe use a Java component that can be instantiated from th