Ask your own question, for FREE!
Computer Science 22 Online
OpenStudy (anonymous):

How can you write code to find a prime factor. I can write code to find factors of a number,but how do you determine if it is a prime factor? Number is 600 billion plus, so need to do it with code.language c++

OpenStudy (osanseviero):

int main() { int n,num,i; printf("enter the num"); scanf("%d",&num); n=num; printf("\n"); for(i=2;i<=n/2;i++) { if(n%i==0) { prime(i); } } } void prime(int x) { int i,f=0; for(i=2;i<=x/2;i++) { if(x%i==0) { f=1; break; } } if(f==0) printf(" %d",x); }

OpenStudy (anonymous):

//Prime Number #include<iostream> #include<conio.h> using namespace std; int main() { int n,i,k; k=0; cout<<" Please Enter n: "; cin>>n; for(i=2;i<n;i++) { if(n%i==0) k=1; } if(k==0) cout<<"prime"; else cout<<"not prime" ; getch(); return 0; }

OpenStudy (anonymous):

I give up!

OpenStudy (osanseviero):

is mine correct?

OpenStudy (anonymous):

I did that once, using C++, doesn't matter, what matter is the logic: This is what I basically did: //To determine if the number entered is prime or not. bool Primos::primo(unsigned num) { // long j = static_cast<long>(sqrt(static_cast<float> (num)) ); if(num < 0) return false; if(num == 1) return false; if(num == 2) return true; if(num % 2 == 0) return false; for(int i = 3; i <= j; i += 2) { if(!(num % i)) return false; } return true; } and another function to display in screen if what prime or not: // Prints the actual prime number and those that are not too to the screen and to an archive of output. unsigned imprime(ofstream& salida) { Primos unPrimo; unsigned numero = entraNumero(); if (numero > 0) { if ( unPrimo.primo(numero) == true ) { cout << "\n\n\t El " << numero << " es un n'umero primo." << "\n\n\n\t" ; salida << "\n\n\t El " << numero << " es un n'umero primo." << "\n\n\n\t" ; system("pause"); } else { cout << "\n\n\t El " << numero << " no es un n'umero primo." << "\n\n\n\t" ; salida << "\n\n\t El " << numero << " no es un n'umero primo." << "\n\n\n\t" ; system("pause"); } } return numero; } Of course I am omitting other functions and code, since I did this using C++, in a OO way, with class header file, definition file and implementation , etc etc... Those are the main things anyways..

OpenStudy (rsmith6559):

First, you find the primes in the range of a the number and store them in a vector or something. Second you iterate through them and see which divide into the number, resulting in another member of the vector.

OpenStudy (anonymous):

yeah, that what I just didm only that U not necesarily need to stored them on a vector, lost, array, I stored them on an output text file as they were show up on the screen

OpenStudy (anonymous):

Usually two methods to find the prime numbers: 1st: Traditional method, This function returns true if n is prime, or false otherwise. #include <math.h> bool isPrime (int n) { if (n<=1) return false; if (n==2) return true; if (n%2==0) return false; int m=sqrt(1.0*n); for (int i=3; i<=m; i+=2) if (n%i==0) return false; return true; } 2nd, Sieve method, This function will return an array, which represent ith number if prime number if it's true, otherwise it's false. This function mainly returns all the prime numbers from 1 to n vector<bool> sieve(int n) { vector<bool> prime(n+1, true); prime[0]=false; prime[1]=false; int m=sqrt(1.0*n); for (int i=2; i<=m; i++) if (prime[i]) for (int k=i*i; k<=n; k+=i) prime[k]=false; return prime; }

OpenStudy (anonymous):

#include<iostream.h> int fact(int x); void main() { char y; do { int x; cout<<"plz,enter no."<<endl; cin>>x; cout<<fact(x)<<endl; cout<<"do u want to try again(y/n)?"; a: cin>>y; if (y!='y' && y!='n') { cout<<"plz enter y/n"; goto a; } } while(y=='y'); } int fact(int x) { int i,f=1; for(i=1;i<=x;i++) f*=i; return f; } OR use this #include <iostream> using namespace std; int main() { int nLoopCount = 0; //declare variables int nFirst ; cout << "Enter Number to be factored:\n"; //get info to and from the user cin >> nLoopCount; //if the user enters a huge number this will keep //their machine from going into a long loop if(nLoopCount > 36000){ nLoopCount = 0; cout << "Number to be factored must be less than 36001:\n"; } for (int j=1; j <= nLoopCount; j++){ //loops through int and stops a point set by nLoopCount for (int i=1; i <=nLoopCount; i++){ //loops through the other number in the factors nFirst = j*i; //multiplies the 2 numbers together nLoopCount times if(nFirst <= nLoopCount && nFirst > nLoopCount-1){ //sort out the numbers we want cout << j <<" * "<< i <<" = "<< nFirst << " sum of factors = "<< j+i << " difference of factors = " << j-i << " , " << i-j << "\n"; //display result } } } return 0; }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!