-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy path1447-SimplifiedFractions.cs
More file actions
37 lines (32 loc) · 966 Bytes
/
1447-SimplifiedFractions.cs
File metadata and controls
37 lines (32 loc) · 966 Bytes
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
//-----------------------------------------------------------------------------
// Runtime: 368ms
// Memory Usage: 41.5 MB
// Link: https://leetcode.com/submissions/detail/371105222/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _1447_SimplifiedFractions
{
public IList<string> SimplifiedFractions(int n)
{
var result = new List<string>();
for (int i = 2; i <= n; i++)
for (int j = 1; j < i; j++)
{
if (IsCooprime(i, j))
result.Add($"{j}/{i}");
}
return result;
}
private bool IsCooprime(int a, int b)
{
return GCD(a, b) == 1;
}
private int GCD(int a, int b)
{
if (a == 0) return b;
return GCD(b % a, a);
}
}
}