document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*Name: Muhammad Azri bin Jasni @ Abdul Rani
  2. **http://projecteuler.net/problem=3
  3. **The prime factors of 13195 are 5, 7, 13 and 29.
  4. **What is the largest prime factor of the number 600851475143?
  5. */
  6.  
  7. /* http://www.physicsforums.com/showthread.php?t=404653
  8. Something to keep in mind is that you don\'t have to check all of the integers from 2 to num.
  9. It is sufficient to go up to sqrt(num) - or the largest integer greater than or equal to sqrt(num).
  10. The idea is that if num has a factor smaller than sqrt(num), then there will be one larger than sqrt(num) as well.
  11.  
  12. For example, suppose num is 85. All you need to do is check the integers from 2 though 9.
  13.  
  14. Check 2 - not a divisor.
  15. Check 3 - not a divisor.
  16. Check 4 - not a divisor.
  17. Check 5 - is a divisor. (The other divisor is 17.)
  18. Check 6 - not a divisor.
  19. Check 7 - not a divisor.
  20. Check 8 - not a divisor.
  21. Check 9 - not a divisor.
  22.  
  23. FYI, you don\'t actually need to check 4, 6, 8, 10, etc. If 2 is not a divisor,
  24. then no other even integer will be a divisor. The same is true for 6, 9, 12, etc.
  25. If 3 isn\'t a divisor, then neither will any other multiple 3 be a divisor.
  26. There are lots of ways to make the program more efficient, but they add complexity to the code.
  27. */
  28. #include <iostream>
  29. using namespace std;
  30.  
  31. int main()
  32. {
  33.     long long num;
  34.     cout << "Enter a number:";
  35.     cin >> num;
  36.    
  37.     for (long long i=2; i<=num ; i++)
  38.     {
  39.         while (num % i == 0)
  40.          {
  41.                num /= i;
  42.                cout << i << " ";
  43.          }
  44.     }
  45.     cout << endl;
  46.     //cin.ignore(11000,\'\\n\');
  47.     //cin.get();
  48.     return 0;
  49. }
');