|
On November 05 2012 13:31 frogmelter wrote:Show nested quote +On November 05 2012 13:25 obesechicken13 wrote:1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop If I could do this I'd be a millionaire. It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds]. Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2. I'm not trolling. Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times. The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so. Ok. well you seem to know more than I gave you credit for then.
I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
There is no known useful formula that yields all of the prime numbers and no composites.
http://en.wikipedia.org/wiki/Prime_number
|
On November 05 2012 13:35 obesechicken13 wrote:Show nested quote +On November 05 2012 13:31 frogmelter wrote:On November 05 2012 13:25 obesechicken13 wrote:1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop If I could do this I'd be a millionaire. It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds]. Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2. I'm not trolling. Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times. The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so. Ok. well you seem to know more than I gave you credit for then. I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that: Show nested quote +There is no known useful formula that yields all of the prime numbers and no composites. http://en.wikipedia.org/wiki/Prime_number
http://en.wikipedia.org/wiki/Miller–Rabin_primality_test
Try that test
And yes, wikipedia is correct. What we are doing is a smarter brute force approach. At best we can try to mitigate the worst case run times, but there are some cases where the run time for the particular algorithm I posted will be O(sqrt(n)/2), which in some cases is still too high
|
On November 05 2012 13:36 frogmelter wrote:Show nested quote +On November 05 2012 13:35 obesechicken13 wrote:On November 05 2012 13:31 frogmelter wrote:On November 05 2012 13:25 obesechicken13 wrote:1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop If I could do this I'd be a millionaire. It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds]. Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2. I'm not trolling. Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times. The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so. Ok. well you seem to know more than I gave you credit for then. I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that: There is no known useful formula that yields all of the prime numbers and no composites. http://en.wikipedia.org/wiki/Prime_number http://en.wikipedia.org/wiki/Miller–Rabin_primality_testTry that test No. Good night. I'll do sieve when I feel like it in the future.
|
On November 05 2012 13:37 obesechicken13 wrote:Show nested quote +On November 05 2012 13:36 frogmelter wrote:On November 05 2012 13:35 obesechicken13 wrote:On November 05 2012 13:31 frogmelter wrote:On November 05 2012 13:25 obesechicken13 wrote:1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop If I could do this I'd be a millionaire. It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds]. Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2. I'm not trolling. Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times. The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so. Ok. well you seem to know more than I gave you credit for then. I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that: There is no known useful formula that yields all of the prime numbers and no composites. http://en.wikipedia.org/wiki/Prime_number http://en.wikipedia.org/wiki/Miller–Rabin_primality_testTry that test No. Good night. I'll do sieve when I feel like it in the future.
Good night, and good luck
|
Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).
Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.
But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing?
|
On November 05 2012 16:45 teamamerica wrote: Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).
Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.
But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing? Fermat's probable prime theorem is probable. You have to repeat the test for various moduli.
Even then, it's ridiculously fast to repeat ~100 times to get good confidence.
Note that Carmichael Numbers break Fermat's Little Theorem, but I believe the density of Carmichael numbers goes down for big numbers, so this can be ignored for applications like RSA.
|
On November 05 2012 17:25 mmp wrote:Show nested quote +On November 05 2012 16:45 teamamerica wrote: Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).
Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.
But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing? Fermat's probable prime theorem is probable. You have to repeat the test for various moduli. Even then, it's ridiculously fast to repeat ~100 times to get good confidence. Note that Carmichael Numbers break Fermat's Little Theorem, but I believe the density of Carmichael numbers goes down for big numbers, so this can be ignored for applications like RSA.
Oh whoa, I never knew it was probable - also never knew about Carmicheal numbers (or that Fermats thm. failed at all TT). Well at least tomorrow when I ask my teacher some questions I'll look like I've actually been doing the reading!
Anyway, I saw some posts about learning C++ - I'm always an advocate for good books, esp. in languages where it's easy to do stuff the wrong way. So there's http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
|
On November 04 2012 17:29 spinesheath wrote:Show nested quote +On November 04 2012 07:25 Lysenko wrote:On November 04 2012 04:15 CecilSunkure wrote:On November 04 2012 02:09 Sawamura wrote: now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5 I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C. I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver? I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++. ** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it. You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C. That's no reason to start with C as your first language though.
I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff.
|
On November 04 2012 17:29 spinesheath wrote:Show nested quote +On November 04 2012 07:25 Lysenko wrote:On November 04 2012 04:15 CecilSunkure wrote:On November 04 2012 02:09 Sawamura wrote: now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5 I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C. I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver? I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++. ** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it. You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C. That's no reason to start with C as your first language though. maybe in germany, in the US old codebases hardly ever use C
|
On November 06 2012 00:24 dakalro wrote:Show nested quote +On November 04 2012 17:29 spinesheath wrote:On November 04 2012 07:25 Lysenko wrote:On November 04 2012 04:15 CecilSunkure wrote:On November 04 2012 02:09 Sawamura wrote: now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5 I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C. I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver? I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++. ** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it. You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C. That's no reason to start with C as your first language though. I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff. very true
|
On November 06 2012 01:56 nath wrote:Show nested quote +On November 04 2012 17:29 spinesheath wrote:On November 04 2012 07:25 Lysenko wrote:On November 04 2012 04:15 CecilSunkure wrote:On November 04 2012 02:09 Sawamura wrote: now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5 I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C. I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver? I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++. ** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it. You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C. That's no reason to start with C as your first language though. maybe in germany, in the US old codebases hardly ever use C
As an american engineer who codes entirely in C, I disagree.
Also, I help build a corporate C compiler, ask me anything about those silly optimizations you want, though, under the hood its really just black magic.
|
On November 06 2012 00:24 dakalro wrote:Show nested quote +On November 04 2012 17:29 spinesheath wrote:On November 04 2012 07:25 Lysenko wrote:On November 04 2012 04:15 CecilSunkure wrote:On November 04 2012 02:09 Sawamura wrote: now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5 I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C. I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver? I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++. ** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it. You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C. That's no reason to start with C as your first language though. I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff.
To all the folks who keep bringing up embedded applications in response to my comments about C largely not being used today (and the comments on my comments) please note that embedded applications are one of the specific examples (among others) I mentioned that would benefit from learning C. ![](/mirror/smilies/smile.gif)
|
United States10328 Posts
bros bros there exist polynomial-time primality tests like http://en.wikipedia.org/wiki/AKS_primality_test
factoring, on the other hand, is not known to be in P (which is why RSA hasn't been broken, yet)
(in fact a lot of cryptography relies on stuff which is not proven, such as: "factoring is hard"; "discrete log is hard"; and "there exists a pseudorandom number generator" [which is true if "discrete log is hard"])
On November 05 2012 17:25 mmp wrote: Fermat's probable prime theorem is probable. You have to repeat the test for various moduli.
hmm, I guess it's probabilistic if you choose random guys a and take a^n-1... but yeah the converse of Fermat's Little Theorem is not necessarily true.
|
Wiki says the original test had a worst case of Õ(log^12(n)) So that's like log(log(log(log...log(n)))))...))) 12 iterated logarithms. I had to go on wolfram to brush up on iterated logarithms. In that case checking for primes is pretty damn fast.
The Euler problem in question though was considered the third easiest question on the site. I highly doubt everyone who solved the problem knew AKS primality test considering the next few problems look really easy in comparison.
|
my solution to #5 used a simple bubble sort :D + Show Spoiler +#include <stdio.h> /* printf */
/* Swap the contents of two pointers. Do not modify the prototype. Use your function from PointerPractice_3 here. */ void SwapInts(int *a,int *b ) { /* Place your code here. Modify these lines. */ int temp = *a; *a = *b; *b = temp; }
/* Print contents of an array. Do not modify this function. */ void PrintArray( const int* array, const int size ) { int i; for(i = 0; i < size; ++i, ++array) { if(i != size - 1) printf( "%d, ", *array); else printf( "%d\n", *array); } }
int main( void ) { /* Create array on the stack. */ int stackArray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
/* Get size of array. stackArray can be treated as pointer to first element. */ int arraySize = sizeof( stackArray ) / sizeof( *stackArray );
/* Can treat name of an array as pointer to first element. */ PrintArray( stackArray, arraySize );
/* Reverse the array here */ /**/ /**/ /**/ int i, j; for (i = (arraySize - 1); i > 0; i--) { for (j = 1; j <= i; j++) { if (stackArray[j-1] < stackArray[j]) { SwapInts(&stackArray[j-1], &stackArray[j]); } } }
PrintArray( stackArray, arraySize );
/* Correct output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 */
return 0; }
|
So, I know what it's finding... But I don't really know how it's finding it...
int arraySize = sizeof( stackArray ) / sizeof( *stackArray );
Can someone break down how exactly this is finding the arraySize for me please?
|
On December 20 2012 03:04 SeeDLiNg wrote: So, I know what it's finding... But I don't really know how it's finding it...
int arraySize = sizeof( stackArray ) / sizeof( *stackArray );
Can someone break down how exactly this is finding the arraySize for me please? Sure!
So with an array on the stack C actually knows the number of elements in the array. So when you do sizeof( stackArray ) the stackArray has not yet implicitly decayed into a pointer, and is actually being treated as a proper array. C multiplies the size of a single element in the array by the number of arrays, and this returns the number of bytes the entire array takes up within memory.
So then sizeof( *stackArray ) is implicitly decaying the stackArray identifier into a pointer and then dereferencing it. The value of *stackArray is that of the first element in the array, since an array's identifier can always be implicitly decayed into a pointer to the first element. So then the size of the first element is 4 bytes: the size of the integer first element.
Then you divide the size of bytes in total by the size in bytes of the first element to get the number of elements total.
|
|
|
|