![]() |
[c++] Finding prime factors
Am I allowed to show off in this board?
For the record, I taught myself VB and C++. ASP is also just VB+HTML... well anyway... I have a friend who's doing mechanical engineering and as part of his curriculum he has to take these online C++ courses. He's fairly clueless when it comes to actual programming, even though he's a genius when comes to pure mathematics. So he asked me if I could write him a program that would take an input and output it's prime factors. Now, I did it in under an hour, and I am quite proud of what I did because the last time I touched C++ was highschool, which was at least two years ago. Code:
#include <stdio.h> |
Code:
Hello, world. |
That doesn't look too much like C++ to me. More like C. Good job for someone who hasn't programmed for a while.
|
Thanks for the tip Slimsam1.
You're right, it's not C++ since I didn't use any classes. It's been so long that I didn't even remember the distinction between C and C++ until you pointed it out Vinaur :thumbsup: |
Laugh, that is C code. =)
A quick way to find prime factors is as follows: 1> Find all prime numbers up to sqrt(x). To find all prime numbers up to sqrt(x): +Try to factor numbers up to sqrt(x). Those with 1 factor are prime. You do the above dynamically. To save on work, you only check numbers equal to 1 and 5 mod 6 for primeness. Code:
enum {max_primes = 10000}; I avoided using std::vector's, because newbies find those confusing. I didn't check array size limits. I never compiled the above. Error handling is minimal. It shouldn't crash. Anyone see any mistakes? |
I made a fun version of this.
Code:
#include <iostream> An interesting aside: It displays the complete prime factorization (with prime repeats), but the primes vector now contains only the unique primes that divide the number. |
Just for giggles and grins, here's the same thing in assembler.
:D ---------------------------------------------------------------- Quote:
|
Ahh assembly! Brings back the good old days during my undergrad where I programmed frogger in assembly ;)
|
Somehow, I still like mine best since it is the shortest, and it never fails.
|
A couple optimizations for you stiltzkin
when you find a prime remove as many of that prime as possible from the list then start searching at that number plus 2, don't start back at 2. Also don't increment i by one instead increament it by 2 (making sure it is odd to start). Here is what I came up with in 3 minutes. I don't actually save the primes but one could easily add the code to save them in a few minutes. Code:
#include <iostream> |
Sieve of Eratosthenes
The Sieve of Eratosthenes is your friend when it comes to primes
http://ccins.camosun.bc.ca/~jbritton/jberatosthenes.htm |
Lebell - an evil genius! Assembly is one of those things I've always been aware of, but just a little too scared to try myself.
|
Quote:
Admittedly, mine is order n/lg(n). And you could maybe RunLengthEncode the Sieve data structure to make it use less memory. . . |
Now just let the programs run on numbers on the order of 1024-bits and you'll be a codebreaker.....
|
I'll see your 49 line program Stiltzkin, and raise you a 43 line variant.
Quote:
|
Just because it doesn't use classes doesn't mean it's not C++. As Bjarne Stroustrup would tell you, C++ is a multiparadigm programming language, which includes procedural programming like C and object oriented programming. That said, the original code was still closer to C since it used only the C library.
|
All times are GMT -8. The time now is 04:21 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project