Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:43:55+00:00 2026-06-06T17:43:55+00:00

Right now I am trying to solve Project Euler 71 . Consider the fraction,

  • 0

Right now I am trying to solve Project Euler 71.

Consider the fraction, n/d, where n and d are positive integers. If
n

If we list the set of reduced proper fractions for d ≤ 8 in ascending
order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8,
2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that 2/5 is the fraction immediately to the left of
3/7.

By listing the set of reduced proper fractions for d ≤ 1,000,000 in
ascending order of size, find the numerator of the fraction
immediately to the left of 3/7.

The Current Code:

from fractions import Fraction
import math

n = 428572
d = 1000000

x = Fraction(3,7)

best = Fraction(0)

while d > 1:
    if Fraction(n,d) >= x:
        n-=1
    else:
        y = Fraction(n,d)
        if (x - y) < (x - best):
            best = y
        d -= 1
        n = int(math.ceil(d*0.428572))

print(best.denominator)

Explanation:

from fractions import Fraction
import math

Needed for Fractions and math.ceil.

n = 428572
d = 1000000

These two variables represent the n and d stated in the original problem. The numbers start out this way because this is a slightly bigger representation of 3/7 (will be converted to Fraction later).

x = Fraction(3,7)

best = Fraction(0)

x is just a quick reference to Fraction(3,7) so I don’t have to keep typing it. best is used to keep track what fraction is closest to 3/7 but still left of it.

while d > 1:

If d <= 1 and n has to be less than 1 what is the point of checking? Stop check then.

if Fraction(n,d) >= x:
    n-=1

If the fraction ends up being bigger than or equal to 3/7 it isn’t to the left of it, so keep subtracting from n till it is to the left of 3/7.

    else:
        y = Fraction(n,d)
        if (x - y) < (x - best):
        best = y

If it is to the left of 3/7 see if 3/7 minus best or y (which is equal to the fraction we need to check) is closer to 0. The one closer to zero will be the least left, or closest to 3/7.

        d -= 1
        n = int(math.ceil(d*0.428572))

Regardless of whether best changes or not, the denominator needs to be changed. So subtract one from the denominator and set n the Fraction(n,d) slightly greater (added extra ceil method to make sure it is greater!) than 3/7 to prune the test space.

print(best.denominator)

Finally print what the question wants.

Note

Changing d to 8 and n to 4 (like the test case) gives the desired result of 5 for the denominator. Keeping it as is gives: 999997.

Can someone please explain to me what I am doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:43:56+00:00Added an answer on June 6, 2026 at 5:43 pm

    What you are doing wrong:

    find the numerator

    Apart from that, follow @Antimony’s advice and learn about the Stern-Brocot tree, that’s useful and fun.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I'm trying to set the default audio device on Windows 7 programmatically,
I'm trying to solve Project Euler #9, which is http://projecteuler.net/index.php?section=problems&id=9 . I've looked through
I am trying to solve Project Euler Problem 88 , and I did it
Last night I was trying to solve challenge #15 from Project Euler : Starting
Right now I'm trying to create my own tiny MVC (just for practice and
Right now I'm trying desperately to get @font-face to work in my website. This
Right now I'm trying this: int a = round(n); where n is a double
Right now I'm trying to write a function to moving up frame when keyboard
Right now I have been trying to use Launchpad's API to write a small
Right now I'm just trying to test my app on my phone and not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.