-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffIntFactorTriplet.cpp
59 lines (48 loc) · 1.83 KB
/
DiffIntFactorTriplet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// DiffIntFactorTriplet.cpp : This file contains the 'main' function. Program execution begins and ends there.
// MIT License (see the licence in the repo for details)
// Copyright (c) 2019 Félix An,
// ported from DiffIntFactorTriplet.py , copyright (c) 2018 Félix An
//
// This script is used to find 3 DIFFERENT positive or negative factors for a given number.
// It was originally written for me to quickly finish a grade 8 math problem.
// This is a port of my original Python script, and runs much faster than it.
#include <iostream>
#include <string>
#include <chrono>
using namespace std;
using namespace std::chrono;
int main()
{
// Disable sync with stdio to speed it up
ios_base::sync_with_stdio(false);
int targetNo;
cout << "Target Number to find factor triplets? Must be a positive integer -> ";
cin >> targetNo;
int int1 = targetNo * -1;
int int2 = targetNo * -1;
int int3 = targetNo * -1;
cout << "Starting query for target " + to_string(targetNo) + ", all three factors will be different...\n";
auto startTime = high_resolution_clock::now();
// work through int1, int2 and int3 clock-style
while (int3 <= targetNo) {
if (int1 * int2 * int3 == targetNo && int1 != int2 && int2 != int3 && int1 != int3) {
cout << to_string(int1) + " * " + to_string(int2) + " * " + to_string(int3) + " = " + to_string(targetNo) + "\n";
}
if (int1 > targetNo) {
int2 = int2 + 1;
int1 = targetNo * -1;
}
else {
int1 = int1 + 1;
}
if (int2 > targetNo) {
int3 = int3 + 1;
int2 = targetNo * -1;
}
}
auto endTime = high_resolution_clock::now();
auto totalTime = duration_cast<nanoseconds>(endTime - startTime);
cout << "Elapsed time: approximately " + to_string(totalTime.count() / 1000000000.0) + " seconds\n";
cout << "*** FINISHED ***\n";
return 0;
}