• We just launched and are currently in beta. Join us as we build and grow the community.

Factorize Number with Vector and Pair

lena71

Save Scummer
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
169
Likes
118
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
We'll see how you can factorize a natural number, using vector and pair from the C++ STL!

First, you need the following declarations:
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>

  4. using

    namespace

    std;

  5. vector<

    pair<

    int

    , int

    >

    >

    factors;
  6. int

    n;

The pairs will contain:
- first element is the DIVISOR
- second element is the EXPONENT

Having these settled, let's move on to reading the number and starting the factorization! We will begin by computing the exponent of 2, because after having 2 as a divisor (in case the number is even), the other divisors are in the form of (2N+1), where N>=1 and N is a natural number.

  1. n =

    0

    ;
  2. cout

    <<

    " x = "

    ;
  3. cin

    >>

    x;
  4. int

    x_original =

    x;
  5. int

    div

    =

    2

    ;
  6. int

    nrd =

    0

    ;
  7. while

    (

    x %

    2

    ==

    0

    )

    {
  8. nrd++

    ;
  9. x /

    =

    2

    ;
  10. }
  11. if

    (

    nrd >

    0

    )

    {
  12. factors.push_back

    (

    make_pair(

    2

    , nrd)

    )

    ;
  13. }

If we have 2 as a divisor, we add it to our vector. Then, we move on to the next divisors and calculate all exponents simply! This is done like this:

  1. div

    =

    3

    ;
  2. while

    (

    x >

    1

    )

    {
  3. nrd =

    0

    ;
  4. while

    (

    x %

    div

    ==

    0

    )

    {
  5. x /

    =

    div

    ;
  6. nrd++

    ;
  7. }
  8. if

    (

    nrd >

    0

    )

    {
  9. factors.push_back

    (

    make_pair(

    div

    , nrd)

    )

    ;
  10. }
  11. div

    +

    =

    2

    ;
  12. }

This is a pretty simple method, we try with any good divisor and always increase the divisor by 2, to ensure it is (2N+1). We also set the number of divisors back to zero on every new try - this is very important!

Then, we need the output:

  1. bool

    first =

    true

    ;
  2. vector<

    pair<

    int

    , int

    >

    >

    ::

    iterator

    it;
  3. cout

    <<

    endl <<

    x_original <<

    " = "

    ;
  4. for

    (

    it =

    factors.begin

    (

    )

    ;

    it !

    =

    factors.end

    (

    )

    ;

    it++

    )

    {
  5. if

    (

    first ==

    false

    )

    {
  6. cout

    <<

    " * "

    ;
  7. }

    else

    {
  8. first =

    false

    ;
  9. }
  10. cout

    <<

    (

    *

    it)

    .first

    <<

    "^"

    <<

    (

    *

    it)

    .second

    ;
  11. }

To avoid confusion or ugly output, such as an extra '*' in the end, we have created a bool variable and we always output the '*' at the beginning of the for loop. If you don't add it like that, you will see a wrong output or need to use control character '\b' to remove the unwanted characters.

The output is simple, as we use an iterator to loop through the vector. Due to the way we computed the factors, they are already sorted from low to high.

The full main.cpp source code is here:

  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>

  4. using

    namespace

    std;

  5. vector<

    pair<

    int

    , int

    >

    >

    factors;
  6. int

    n;

  7. int

    main(

    int

    argc, char

    **

    argv)

    {
  8. int

    x;
  9. n =

    0

    ;
  10. cout

    <<

    " x = "

    ;
  11. cin

    >>

    x;
  12. int

    x_original =

    x;
  13. int

    div

    =

    2

    ;
  14. int

    nrd =

    0

    ;
  15. while

    (

    x %

    2

    ==

    0

    )

    {
  16. nrd++

    ;
  17. x /

    =

    2

    ;
  18. }
  19. if

    (

    nrd >

    0

    )

    {
  20. factors.push_back

    (

    make_pair(

    2

    , nrd)

    )

    ;
  21. }
  22. div

    =

    3

    ;
  23. while

    (

    x >

    1

    )

    {
  24. nrd =

    0

    ;
  25. while

    (

    x %

    div

    ==

    0

    )

    {
  26. x /

    =

    div

    ;
  27. nrd++

    ;
  28. }
  29. if

    (

    nrd >

    0

    )

    {
  30. factors.push_back

    (

    make_pair(

    div

    , nrd)

    )

    ;
  31. }
  32. div

    +

    =

    2

    ;
  33. }

  34. bool

    first =

    true

    ;
  35. vector<

    pair<

    int

    , int

    >

    >

    ::

    iterator

    it;
  36. cout

    <<

    endl <<

    x_original <<

    " = "

    ;
  37. for

    (

    it =

    factors.begin

    (

    )

    ;

    it !

    =

    factors.end

    (

    )

    ;

    it++

    )

    {
  38. if

    (

    first ==

    false

    )

    {
  39. cout

    <<

    " * "

    ;
  40. }

    else

    {
  41. first =

    false

    ;
  42. }
  43. cout

    <<

    (

    *

    it)

    .first

    <<

    "^"

    <<

    (

    *

    it)

    .second

    ;
  44. }
  45. cout

    <<

    endl;
  46. return

    0

    ;
  47. }

The output will be something like this:

x = 120

120 = 2^3 * 3^1 * 5^1

Enjoy!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,156

324,164

Top