| | |
C++ Factorization
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 1
Reputation:
Solved Threads: 0
I've been working on a program and have hit a roadblock. I know conceptually what to do, but I'm having trouble implimenting it.
Ok, so what it's suppose to do is take them randomly generated numbers that have been put in arrayints and factor them. This right now for an example if the number is 100 it will print out: 2 * 50 4 * 25 5 * 20 10 * 10 1 * 100. What I want it to print out is: 2 * 2 * 5 * 5 (doesn't have to be in that order I guess) but I'm having trouble with this. It's probably something simple that I'm overlooking but if anybody could help with it that would be great. Thanks!
Conceptually, I know I need to take the number I get from the if((arrayints[i] % j) == 0 && arrayints[i] != j) and run it back through here. Ex. 100, you would get factor1 = 2, and factor2 = 50, so I need to somehow run factor2 back through the if statement without incrementing j. (hopefully you understand what I mean lol)
Ok, so what it's suppose to do is take them randomly generated numbers that have been put in arrayints and factor them. This right now for an example if the number is 100 it will print out: 2 * 50 4 * 25 5 * 20 10 * 10 1 * 100. What I want it to print out is: 2 * 2 * 5 * 5 (doesn't have to be in that order I guess) but I'm having trouble with this. It's probably something simple that I'm overlooking but if anybody could help with it that would be great. Thanks!
Conceptually, I know I need to take the number I get from the if((arrayints[i] % j) == 0 && arrayints[i] != j) and run it back through here. Ex. 100, you would get factor1 = 2, and factor2 = 50, so I need to somehow run factor2 back through the if statement without incrementing j. (hopefully you understand what I mean lol)
c++ Syntax (Toggle Plain Text)
for (int i = 0; i < integerIn; i++) { cout << "The factors for " << arrayints[i] << " are: "; for (int j = 2; j <= floor (sqrt((double)integerIn)); j++) { if ((arrayints[i] % j) == 0 && arrayints[i] != j) { factor1 = j; factor2 = arrayints[i]/j; cout << factor1 << " * " << factor2 << " "; } } factor1 = 1; factor2 = arrayints[i]; cout << factor1 << " * " << factor2 <<endl; }
First, please learn to Format your code so we can read it easier.
You need to store each factor in an array as you generate them. Using your 100, you first generate 2. Save it in the array and you have 50 left. Run the loop again on the 50 and you'll get 2 again. Store it with the other 2. You have 25 left. Again and you'll get the 5. Store it. You loop on the 5 and discover it can't be reduced. Store it with the rest then output the array.
You need to store each factor in an array as you generate them. Using your 100, you first generate 2. Save it in the array and you have 50 left. Run the loop again on the 50 and you'll get 2 again. Store it with the other 2. You have 25 left. Again and you'll get the 5. Store it. You loop on the 5 and discover it can't be reduced. Store it with the rest then output the array.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2008
Posts: 46
Reputation:
Solved Threads: 11
First,for a a number in an array call it as
C++ Syntax (Toggle Plain Text)
int num;
c Syntax (Toggle Plain Text)
//Your expression for evaluating control variable //certainly doesn't give all the factors,referring to //for (int j = 2; j <= floor (sqrt((double)integerIn)); j++) //try for integerIn=999; //999's square root lying between 31 and 32 while one of its prime //factors(read major) being '37' //999 = 3*3 *3 *37 //Doesn't mean mine is the fastest(just works) //Store the factors if its required for (int j=2 ; j<= num; j++) { //Just as WaltP suggested go on dividing unless you //get a prime number while (num % j==0) { cout <<num; num/=j ; } }
Last edited by zalezog; Feb 21st, 2009 at 1:38 pm.
![]() |
Similar Threads
- factoring function for large numbers (C++)
- Matlab: Solving linear system with QR (Legacy and Other Languages)
- Searching a file for a string (C++)
- needed recursion help (C)
- Reverse Output (Stack) (C++)
Other Threads in the C++ Forum
- Previous Thread: curses problem
- Next Thread: Best C++ compiler
Views: 875 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






