cout << "Your number is a 0, 1, or 2. These cannot be prime."<< endl;
Not true. 2 is prime. [safety net]as far as I know[/safety net]
Sorry for the unconstructive criticism, I just had to...
Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo
cout << "Your number is a 0, 1, or 2. These cannot be prime."<< endl;
WaltP wrote:schloob wrote:Colin Jeanne wrote:schloob wrote:anyways, comments just arent my style :-\
They'll get to be.
you wish :-]
Hey, leave shloob alone. Someone that never wishes to be a professional programmer as shloob obviously doesn't has no need to learn proper coding techniques. Many people stay beginners and hacks, leaves the big money to us pros.
#include <iostream>
#include <vector>
// Finds the factors of n and returns them as a vector
std::vector<unsigned> factors(unsigned n){
std::vector<unsigned> f;
// Take care of some trivial cases first
if(n<4){
f.push_back(n);
return f;
}
// Try to divide by all numbers until sqrt(n) is reached
for(unsigned k=2;k*k<=n;++k){
// Extract as many factors k as we can
while(n%k==0){
f.push_back(k);
n/=k;
}
}
// If at the end n still has something in it, it must be prime, so just add it.
if(n>1)
f.push_back(n);
return f;
}
void print_vector(std::ostream &os, const std::vector<unsigned> &v, const char *sep){
if(v.size()==0)
return;
os << v[0];
for(size_t i=1;i<v.size();++i)
os << sep << v[i];
}
int main(){
std::cout << "Enter a number: " << std::flush;
unsigned n;
std::cin >> n;
std::cout << n << " = ";
print_vector(std::cout, factors(n)," x ");
std::cout << std::endl;
}
#include<iostream>
using namespace std;
int main(){cout<<"Enter a number: "<<flush;unsigned o=2,o_;cin>>o_;cout
<<o_;char O[]=" = ";if(o_<4)return cout<<O<<o_<<'\n',0;for(;o_>=o*o;++o
)for(;o_%o==0;cout<<O<<o,1[O]='x')o_/=o;o_>1&&cout<<O<<o_;cout<<'\n';}Alvaro wrote:Note that you don't have to check each factor you find for primality: the smallest factor of an integer is always prime (easy theorem). You can speed up the algorithm by only trying to divide by 2 and then odd numbers, or by 2, 3 and multiplies of 6 plus or minus 1. I left that out to keep things simple.
Alvaro wrote:Here is an alternative:
- Code: Select all
#include<iostream>
using namespace std;
int main(){cout<<"Enter a number: "<<flush;unsigned o=2,o_;cin>>o_;cout
<<o_;char O[]=" = ";if(o_<4)return cout<<O<<o_<<'\n',0;for(;o_>=o*o;++o
)for(;o_%o==0;cout<<O<<o,1[O]='x')o_/=o;o_>1&&cout<<O<<o_;cout<<'\n';}
#include <iostream>
using namespace std;
long primenext(long* prime);
int main()
{
for(;;)
{
int answer;
cout << endl;
cout << "Enter a number for Prime Factors : " << endl;
cin >> answer;
long prime = 2;
long factor = 0;
if (answer > 0)
{
do
{
if (answer % prime == 0)
{
factor = answer / prime;
if (answer == prime)//the end if they are ==
answer = 0;
else
answer = factor;
cout << prime << ((0 == answer) ? "." : "x");
}
else
{
//next++ will decide the array number
primenext(&prime);//get next prime
}
}while(answer > 0);
}
else
{
cout << "That was out of range." << endl;
return 0; //check for valid input
}
}
}
long primenext(long* prime) //prime number generator
{
if(*prime == 2)// New and improved
*prime = 3;
else
{
*prime += 2;
}
return *prime;
}#include <iostream>
using namespace std;
int main()
{
int UserNum;
cout << "Will break a number down into it's prime factors\n";
cout << "Enter your number: \n";
cin >> UserNum;
if(RaH == STUMPED)
{
cout << "Insert clue here\n";
}
return 0;
}
Alvaro wrote:Note that you don't have to check each factor you find for primality: the smallest factor of an integer is always prime (easy theorem). You can speed up the algorithm by only trying to divide by 2 and then odd numbers, or by 2, 3 and multiplies of 6 plus or minus 1. I left that out to keep things simple.
Alvaro wrote:I'll try to explain with an example, n=150.
I look for factors of 150, starting with 2. I divide 150 by 2 as many times as I can (once). So I can already write "2 x ", and what follows will be a factorization of 75. In my algorithm, I destroy the old value of n as I go, taking out all the factors that I have found so far.
Now n=75, and I have to look for factors. I don't have to try 2 again, because I already extracted as much as I could. So I try with 3. So I now have "2 x 3 x ", and now I have to factorize 25.
n=25, and I don't have to try 2 or 3. I try 4, although there is no need, because 4 is not prime, which means that we already know that you can't divide 25 by 4 (otherwise 25 would be divisible by some prime smaller than 4, which we have already rooted out). Then I try 5, and I see that it divides twice. Now I have "2 x 3 x 5 x 5" and the factorization is over, because n=1.
If the largest factor only appears once, then the loop will be aborted when it is clear that the remaining n is prime, so we have to print it.
Wow! So much explaining for such a small function. I probably didn't explain it very well, though.
Users browsing this forum: No registered users and 1 guest