Taking integers from input.txt

I am currently stuck on how to differentiate integers from each line in a text file. The input file consists of 2 lines. The first line has two numbers, the second has only one number. I am trying to take the GCD of two numbers in the first line, and then take the single number of the second line and calculate the phi and provide a list of totatives for the number.

A sample input.txt is the following:
1
2
9 3
9


My current code is:

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::cin;

long GreatCD(long num1, long num2)
{
    int remain_der;

    if ((num1 < 0) || (num2 < 0)) {
        cout << "Error";
    }
    
    do
    {
        remain_der = num1 % num2;
        
        if(remain_der == 0) {
            break;
        }
        num1 = num2;
        num2 = remain_der;
        
    }
    
    while(true);

    return num2;
}

long Phi(long n) {

	long result = n;

	for (int i = 2; i*i <= n; ++i) {

		if (n % i == 0) {
			while (n % i == 0) {
				n /= i;
				result = result/i;
			}
		}
	}

	if (n > 1) {
		result = result/i;

	}

	return result;
}

int main(void)
{
    int num1 = 0, num2 = 0;
    int n = 0;
    int phicalc = Phi(n);

    cin >> num1;
    cin >> num2;
    cin >> n;

    int gcdcalc = GreatCD(num1, num2); 

    cout << gcdcalc;
    cout << phicalc;

    getchar();
    
    return 0;
}


My program currently calculates the GCD just fine, but I can't get the phi calculations to work. Any help would be great, thanks!
closed account (48T7M4Gy)
So where are the program lines for reading in the 3 numbers?
I was just doing cin >> num1; and cin >> num2; because the program is suppose to run as if I was doing the following in console:

./proj02 < input.txt
@PirateCat

It isn't clear whether your problem is reading from file (which is what the title implies) or calculating phi(n), which is what you have written subsequently.

To read from file simply do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int num1, num2, n;

    ifstream in( "input.txt" );
    in >> num1 >> num2 >> n;
    in.close();

    cout << "Numbers are " << num1 << "  " << num2 << "  " << n << endl;
}



The first problem with your calculation of phi(n) is that you call Phi(n) in main() before inputting n.

If you have a working GCD routine (I haven't checked) then you could simply use this to calculate phi(n). Cycle through the integers i less than or equal to n and the totatives are those for which GCD(n,i) = 1: either store them in a vector or just output them as you go along. Phi(n) is the number of such totatives: count them as you find them.

There are probably much more efficient ways: if n is prime, for example, then Phi(n) would simply be n-1 without further calculation.

See:
https://en.wikipedia.org/wiki/Euler's_totient_function

Last edited on
closed account (48T7M4Gy)
I think the introductory sentences from the OP are a dead giveaway that input.txt is the source of the data. The rest is just an indication that there is an attempt being made to take us for a ride.
Topic archived. No new replies allowed.