First off, I'd like to say that I'm sorry I was gone so long and that I'm going to post a blog entry at least once a month from now on no matter what.
Also, the next version of my prime number generator is out now that I fully understand arrays. Here it is in all its 49 lines of glory:
If you'll notice, I have my own sqroot function in here because Math.sqroot() only works on doubles and only returns doubles. Therefore I actually had to declare my own (originating in the previous version). This is made as efficient as possible by starting at 2 (because I'll never be using zero) and working its way UP instead of DOWN all the way to the nearest number to the square root rounded up to the nearest whole number. Interestingly enough, this leads the program to find the square root of 64 as 9, but the square root of 63 as 8. The reason counting up is more efficient than counting down in order to find the square root is because the amount of numbers lower than the square root but above 1 is much less than the amount of numbers higher than the square root but below the number. This same principle also makes using the square root as a start/end point extremely efficient. Counting up to the square root is also more efficient than counting down from the square root, because any number is much more likely to be proven not a prime by using 2 first, then 3, then 5, etc. than starting with the nearest primes below its square root.
Any suggestions on making this program even more efficient will be gleefully accepted.
Also, I've been making a twoplayer threehanded pinochle game for the past month where you play against the computer. There was a point about a week ago where I had to reprogram everything I had done so far except the AI in order to switch the graphics to use arrays, and I'm nearly done now. But I'm having slight problems getting the graphics to appear. I started a topic at http://www.dreaminco...aphicstoshow/ that deals with this. Aside from my problem, I only have small pieces of the AI left to program before I'm ready to test it.
Also, the next version of my prime number generator is out now that I fully understand arrays. Here it is in all its 49 lines of glory:
public class TestedPrimeNumberFinder { public static void main (String args[]) { int[] primes = new int[100]; int prime = 2; primes[0] = prime; System.out.println("Prime number 1: " + prime); prime++; primes[1] = prime; System.out.println("Prime number 2: " + prime); prime++; int primecheck; int j; for (int i = 3; i < primes.length; ) { for (primecheck = (sqroot(prime)), j = 0; primes[j] <= primecheck; ) { if (primes[j] == 0) { primes[i] = prime; System.out.println("Prime number " + i + ": " + primes[i]); prime++; i++; break; } else if (prime % primes[j] != 0) { j++; } else { prime++; break; } } } } public static int sqroot(int i) { int sqrt = 0; for ( ; i > (sqrt * sqrt); ) { sqrt++; } return sqrt; } }
If you'll notice, I have my own sqroot function in here because Math.sqroot() only works on doubles and only returns doubles. Therefore I actually had to declare my own (originating in the previous version). This is made as efficient as possible by starting at 2 (because I'll never be using zero) and working its way UP instead of DOWN all the way to the nearest number to the square root rounded up to the nearest whole number. Interestingly enough, this leads the program to find the square root of 64 as 9, but the square root of 63 as 8. The reason counting up is more efficient than counting down in order to find the square root is because the amount of numbers lower than the square root but above 1 is much less than the amount of numbers higher than the square root but below the number. This same principle also makes using the square root as a start/end point extremely efficient. Counting up to the square root is also more efficient than counting down from the square root, because any number is much more likely to be proven not a prime by using 2 first, then 3, then 5, etc. than starting with the nearest primes below its square root.
Any suggestions on making this program even more efficient will be gleefully accepted.
Also, I've been making a twoplayer threehanded pinochle game for the past month where you play against the computer. There was a point about a week ago where I had to reprogram everything I had done so far except the AI in order to switch the graphics to use arrays, and I'm nearly done now. But I'm having slight problems getting the graphics to appear. I started a topic at http://www.dreaminco...aphicstoshow/ that deals with this. Aside from my problem, I only have small pieces of the AI left to program before I'm ready to test it.
1 Comments On This Entry
Page 1 of 1
Ryano121
08 February 2013  02:46 PM
I don't see why you didn't use Math.Sqrt(). You can simply cast the result back to an int if needed
(int)Math.Sqrt(prime)
Page 1 of 1
Trackbacks for this entry [ Trackback URL ]
← April 2015 →
S  M  T  W  T  F  S 

1  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 
Tags
My Blog Links
Recent Entries

I'm back for good/Prime Number Generators/Pinochle
on Feb 08 2013 06:38 AM
Recent Comments
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
Categories
