/* * Ordered fractions * Problem 71 * * Consider the fraction, n/d, where n and d are positive integers. If n 0 { t = b b = a % b a = t } return a } func hasCommonFactor(a, b int) bool { return hcf(a, b) != 1 } type Fraction struct { N int D int } func (f1 Fraction) isLower(f2 Fraction) bool { ni_common := f1.N * f2.D nj_common := f2.N * f1.D return ni_common < nj_common } var targetFraction = Fraction {N: 3, D: 7} func main() { fractions := make([]Fraction, 0, MAX_DENOMINATOR) fractions = append(fractions, targetFraction) for d := 1; d <= MAX_DENOMINATOR; d++ { lo := 1 hi := d n := (hi + lo) / 2 var f Fraction for lo < hi { factor := hcf(n, d) f = Fraction{ N: n / factor, D: d / factor, } if f.isLower(targetFraction) { lo = n + 1 } else { hi = n } n = (hi + lo) / 2 } fractions = append(fractions, f) // in case we're at the fraction just one above the target fraction f.N-- fractions = append(fractions, f) } sort.Slice(fractions, func (i, j int) bool { return fractions[i].isLower(fractions[j]) }) new_fractions := make([]Fraction, 0, len(fractions)) for i := 0; i < len(fractions); { new_fractions = append(new_fractions, fractions[i]) curr := fractions[i] for i < len(fractions) && curr.N == fractions[i].N && curr.D == fractions[i].D { i++ } } for i, f := range new_fractions { if f.N == targetFraction.N && f.D == targetFraction.D { fmt.Printf("to the left of %d/%d is %d/%d\n", f.N, f.D, new_fractions[i - 1].N, new_fractions[i - 1].D) fmt.Printf("answer %d\n", new_fractions[i - 1].N) } } }