Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N.
Here is the set when N = 5:
Write a program that, given an integer N between 1 and 160 inclusive, prints the fractions in order of increasing magnitude.
Input
One line with a single integer N.
Output
One fraction per line, sorted in order of magnitude.
Examples
Input #1
5
Answer #1
0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1 c++da necə etmək olar sualı?
Assistant
Certainly! Here's a C++ program that solves the problem of generating and sorting all reduced fractions between 0 and 1 (inclusive) with denominators up to a given integer N. The program reads an integer N from the input, generates all valid fractions, sorts them in increasing order of their magnitude, and then prints each fraction on a separate line.
Step-by-Step Explanation
Fraction Representation:
We represent each fraction as a pair of integers (numerator, denominator).
To ensure that the fraction is in its reduced form, we check that the greatest common divisor (GCD) of the numerator and denominator is 1.
Generating Fractions:
Iterate through all possible denominators d from 1 to N.
For each denominator d, iterate through all possible numerators n from 0 to d.
For each pair (n, d), check if gcd(n, d) == 1. If it is, the fraction is in reduced form and is added to the list.
Sorting Fractions:
To sort the fractions without floating-point inaccuracies, we compare two fractions a/b and c/d by cross-multiplying to avoid division:
a/b < c/d is equivalent to a * d < c * b.
This comparison method ensures accurate sorting based on the actual value of the fractions.
Handling Duplicates:
Since we only include fractions in their reduced form, duplicates are naturally avoided.
Output:
After sorting, print each fraction in the format n/d on a separate line.
C++ Implementation
cpp
#include <bits/stdc++.h>
using namespace std;
// Function to compute GCD using Euclidean algorithm
int gcd_func(int a, int b) {
if (b == 0)
return a;
return gcd_func(b, a % b);
}
int main(){
int N;
cin >> N;
// Vector to store fractions as pairs (numerator, denominator)
vector<pair<int, int>> fractions;
for(int d =1; d <=N; d++){
for(int n =0; n <=d; n++){
if(d ==1 || gcd_func(n, d) ==1){
fractions.emplace_back(n, d);
}
}
}
// Remove duplicates if any (though with gcd check, should be unique)
// Not necessary, but safe
sort(fractions.begin(), fractions.end(), [&](const pair<int,int> &a, const pair<int,int> &b)->bool{
// Compare a/b < c/d by cross multiplication to avoid floating point
return (long long)a.first * b.second < (long long)b.first * a.second;
});
// To ensure 0/1 is first and 1/1 is last, but sorting should handle it
for(auto &[n, d]: fractions){
cout << n << "/" << d << "\n";
}
}
How to Use the Program
Input: Provide a single integer N (1 ≤ N ≤ 160) as input.
Output: The program will output all reduced fractions between 0/1 and 1/1 with denominators less than or equal to N, sorted in increasing order.
Example
Input:
code
5
Output:
code
0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1
This matches the example provided, ensuring that the program works correctly.
Compilation and Execution
To compile and run this program, you can use any C++ compiler. Here's how you can do it using g++:
Save the Code: Save the above code in a file named fractions.cpp.
Compile:
bash
g++ -std=c++11 -O2 -o fractions fractions.cpp
Run:
bash
./fractions
Input: Enter the value of N when prompted or provide it through input redirection.
For example:
bash
echo 5 | ./fractions
This will output the expected list of fractions.
Optimization Notes
Efficiency: The program efficiently generates all possible fractions and ensures they are in reduced form by checking the GCD.
Sorting: By using cross-multiplication in the comparator, the program avoids floating-point inaccuracies and ensures correct ordering.
Constraints Handling: Given the constraint 1 ≤ N ≤ 160, the program performs adequately without the need for further optimization.
Feel free to use and modify this program as needed for your specific use case!