943,584 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1982
  • C++ RSS
Feb 20th, 2009
0

C++ Factorization

Expand Post »
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)

c++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nparrish15 is offline Offline
1 posts
since Feb 2009
Feb 21st, 2009
0

Re: C++ Factorization

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.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is online now Online
7,717 posts
since May 2006
Feb 21st, 2009
0

Re: C++ Factorization

First,for a a number in an array call it as
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Feb 22nd, 2009
0

Re: C++ Factorization

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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Feb 22nd, 2009
0

Re: C++ Factorization

Indeed waltp's way i the easiest to find prime factors of a number
Reputation Points: 10
Solved Threads: 5
Light Poster
arghasen is offline Offline
35 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: curses problem
Next Thread in C++ Forum Timeline: please help me with this





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC