C++ Factorization

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 1
Reputation: nparrish15 is an unknown quantity at this point 
Solved Threads: 0
nparrish15 nparrish15 is offline Offline
Newbie Poster

C++ Factorization

 
0
  #1
Feb 20th, 2009
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)

  1. for (int i = 0; i < integerIn; i++)
  2. {
  3. cout << "The factors for " << arrayints[i] << " are: ";
  4. for (int j = 2; j <= floor (sqrt((double)integerIn)); j++)
  5. {
  6. if ((arrayints[i] % j) == 0 && arrayints[i] != j)
  7. {
  8. factor1 = j;
  9. factor2 = arrayints[i]/j;
  10. cout << factor1 << " * " << factor2 << " ";
  11. }
  12. }
  13. factor1 = 1;
  14. factor2 = arrayints[i];
  15. cout << factor1 << " * " << factor2 <<endl;
  16. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ Factorization

 
0
  #2
Feb 21st, 2009
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 46
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: C++ Factorization

 
0
  #3
Feb 21st, 2009
First,for a a number in an array call it as
  1. int num;

  1. //Your expression for evaluating control variable
  2. //certainly doesn't give all the factors,referring to
  3. //for (int j = 2; j <= floor (sqrt((double)integerIn)); j++)
  4. //try for integerIn=999;
  5. //999's square root lying between 31 and 32 while one of its prime
  6. //factors(read major) being '37'
  7. //999 = 3*3 *3 *37
  8. //Doesn't mean mine is the fastest(just works)
  9. //Store the factors if its required
  10.  
  11. for (int j=2 ; j<= num; j++)
  12. { //Just as WaltP suggested go on dividing unless you
  13. //get a prime number
  14. while (num % j==0)
  15. {
  16. cout <<num;
  17. num/=j ;
  18. }
  19. }
Last edited by zalezog; Feb 21st, 2009 at 1:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: C++ Factorization

 
0
  #4
Feb 22nd, 2009
You want to actually find out the PRIME factors i.e. expand the number as the product of prime numbers(2,3,5,7...)
So the easiest way would be what has been told by WaltP.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: arghasen is an unknown quantity at this point 
Solved Threads: 4
arghasen arghasen is offline Offline
Light Poster

Re: C++ Factorization

 
0
  #5
Feb 22nd, 2009
Indeed waltp's way i the easiest to find prime factors of a number
Argha Sen

Ask and it will be opened for u
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 875 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC