1- // DiffIntFactorTriplet.cpp : This file contains the 'main' function. Program execution begins and ends there.
2- // MIT License (see the licence in the repo for details)
3- // Copyright (c) 2019 Félix An,
4- // ported from DiffIntFactorTriplet.py , copyright (c) 2018 Félix An
5- //
6- // This script is used to find 3 DIFFERENT positive or negative factors for a given number.
7- // This is a port of my original Python script, and runs much faster than it.
8-
9- #include < iostream>
10- #include < string>
11- #include < chrono>
12- using namespace std ;
13- using namespace std ::chrono;
14-
15- int main ()
16- {
17- int targetNo;
18- cout << " Target Number to find factor triplets? Must be a positive integer -> " ;
19- cin >> targetNo;
20-
21- int int1 = targetNo * -1 ;
22- int int2 = targetNo * -1 ;
23- int int3 = targetNo * -1 ;
24-
25- cout << " Starting query for target " + to_string (targetNo) + " , all three factors will be different...\n " ;
26-
27- auto startTime = chrono:: high_resolution_clock::now ();
28-
29- while (int3 <= targetNo) {
30- if (int1 * int2 * int3 == targetNo && int1 != int2 && int2 != int3 && int1 != int3) {
31- cout << to_string (int1) + " * " + to_string (int2) + " * " + to_string (int3) + " = " + to_string (targetNo) + " \n " ;
32- }
33-
34- if (int1 > targetNo) {
35- int2 = int2 + 1 ;
36- int1 = targetNo * -1 ;
37- }
38- else {
39- int1 = int1 + 1 ;
40- }
41-
42- if (int2 > targetNo) {
43- int3 = int3 + 1 ;
44- int2 = targetNo * -1 ;
45- }
46- }
47-
48- auto endTime = chrono:: high_resolution_clock::now ();
49- auto totalTime = endTime - startTime;
50- cout << " Time taken by program is : " + to_string (totalTime) ;
51- cout << " *** FINISHED ***\n " ;
52-
53- return 0 ;
1+ // DiffIntFactorTriplet.cpp : This file contains the 'main' function. Program execution begins and ends there.
2+ // MIT License (see the licence in the repo for details)
3+ // Copyright (c) 2019 Félix An,
4+ // ported from DiffIntFactorTriplet.py , copyright (c) 2018 Félix An
5+ //
6+ // This script is used to find 3 DIFFERENT positive or negative factors for a given number.
7+ // This is a port of my original Python script, and runs much faster than it.
8+
9+ #include < iostream>
10+ #include < string>
11+ #include < chrono>
12+ using namespace std ;
13+ using namespace std ::chrono;
14+
15+ int main ()
16+ {
17+ int targetNo;
18+ cout << " Target Number to find factor triplets? Must be a positive integer -> " ;
19+ cin >> targetNo;
20+
21+ int int1 = targetNo * -1 ;
22+ int int2 = targetNo * -1 ;
23+ int int3 = targetNo * -1 ;
24+
25+ cout << " Starting query for target " + to_string (targetNo) + " , all three factors will be different...\n " ;
26+
27+ auto startTime = high_resolution_clock::now ();
28+
29+ while (int3 <= targetNo) {
30+ if (int1 * int2 * int3 == targetNo && int1 != int2 && int2 != int3 && int1 != int3) {
31+ cout << to_string (int1) + " * " + to_string (int2) + " * " + to_string (int3) + " = " + to_string (targetNo) + " \n " ;
32+ }
33+
34+ if (int1 > targetNo) {
35+ int2 = int2 + 1 ;
36+ int1 = targetNo * -1 ;
37+ }
38+ else {
39+ int1 = int1 + 1 ;
40+ }
41+
42+ if (int2 > targetNo) {
43+ int3 = int3 + 1 ;
44+ int2 = targetNo * -1 ;
45+ }
46+ }
47+
48+ auto endTime = high_resolution_clock::now ();
49+ auto totalTime = duration_cast<nanoseconds>( endTime - startTime) ;
50+ cout << " Elapsed time: approximately " + to_string (totalTime. count () / 1000000000.0 ) + " seconds \n " ;
51+ cout << " *** FINISHED ***\n " ;
52+
53+ return 0 ;
5454}
0 commit comments