Rational Approximation of numbers

S = 10 3 S = \sqrt [ 3 ]{ 10 }

Define P = a b P = \dfrac{a}{b} , where a , b a,b are natural numbers less than 1000 1000

Find a + b a+b such that P P has a value closest to S S , or mathematically P S |P-S| is minimised

a , b a,b are co-prime natural numbers.


The answer is 1246.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

9 solutions

Pi Han Goh
Mar 18, 2015

With enough patience, we can get the continued fraction of 10 3 \sqrt[3]{10} to be [ 2 ; 6 , 2 , 9 , 1 , 1 , 2 , 4 , ] [2;6,2,9,1,1,2,4, \ldots ]

So we want to input as much nested fractions, trial and error shows that for constraint for numerator and denominator to be less than 1000 1000 would give the fraction below (with natural number x x )

2 + 1 6 + 1 2 + 1 9 + 1 1 + 1 1 + x = 293 x + 558 136 x + 259 \LARGE 2 + \frac {1}{ 6 + \frac {1}{2 + \frac {1}{9 + \frac {1}{1 + \frac {1}{1+x}}}} } = \frac {293x+558}{136x+259}

Substitute x = 1 x = 1 gives a = 851 , b = 395 a = 851, b = 395

Very nice, if I may say so.

Bill Bell - 6 years, 2 months ago

i also did the same great

Manit Kapoor - 6 years ago
Pranjal Jain
Mar 22, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
S=10**(1.0/3)
def closeness(a,b):
    P=a/b
    if (S>P):
        return S-P
    else:
        return P-S
lista=[]
for b in range(1,500):
    for a in range(2*b,1000):
        lista.append((closeness(a,b),a,b))
lista.sort()
lista[0]

It returns (4.310285048436668e-06, 851, 395)

Here is a beginner's way to solve this problem: brute force. The code below is self explanatory:

 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
#include<iostream.h>
#include<conio.h>
#include<math.h>

//brute irrational root approximator.

int main()
    {
    clrscr();
    double n,a,p;
    cout<<"enter a in a^(n)"<<endl;cin>>a;
    cout<<"enter (1/n) in a^(n)"<<endl;cin>>p;
    n=1/p;n=pow(a,n);
    cout<<"enter upper bound for numerator and denominator"<<endl;
    cin>>a;
    double d=100,num,den;
    for(double i=1;i<=a;i++)
        {
        for(double j=1;j<=a;j++)
            {
            double k=(i/j);
            k=k-n;
            (k>0)?k=k:k=-1*k;
            if(k<d)
                {
                d=k;
                num=i;den=j;
                }
            }
        }
    d=num/den;
    cout<<n<<endl<<d<<" = "<<num<<"/"<<den;
    getch();
    return 0;
    }

I did the same way , I love brute force searches.

Ronak Agarwal - 6 years, 2 months ago

@Raghav Vaidyanathan is this code for netbeans????

Aditya Kumar - 6 years, 1 month ago

Log in to reply

no. C++, i used borland compiler.

Raghav Vaidyanathan - 6 years, 1 month ago

Log in to reply

do u know how to operate on netbeans???

Aditya Kumar - 6 years, 1 month ago

Log in to reply

@Aditya Kumar Sorry, I do not.

Raghav Vaidyanathan - 6 years, 1 month ago

Log in to reply

@Raghav Vaidyanathan thats fine :)

Aditya Kumar - 6 years, 1 month ago

Log in to reply

@Aditya Kumar NetBeans is a compiler isn't it?

Arulx Z - 5 years, 11 months ago

Log in to reply

@Arulx Z NetBeans is an IDE.

Raghav Vaidyanathan - 5 years, 11 months ago

Log in to reply

@Raghav Vaidyanathan Oh yes. Sorry. IDE...

Arulx Z - 5 years, 11 months ago
Aareyan Manzoor
Jun 20, 2015

python 2.7: gives 1246

Seth Lovelace
May 27, 2015

def f(x,d,k):

for a in range(1,k):

    for b in range(1,k):

        P = a/b

        e = abs(P-x)

        if e < d:

            print(e,a,b)

x is the value we wish to find a close fraction for. d is our delta value, i.e. our acceptable margin of error. k is how large our domain of natural numbers should be.

I used Python or this code. Essentially I wrote a double for loop to let Python go through every value of a/b, but ignore those that had a margin of error larger than my delta requirement. I then looked at the printed values and chose the a and b that resulted in the smallest epsilon, or error, value. The "a" and "b" that led to this were 851 and 395.

Arulx Z
May 17, 2015
1
2
3
4
5
6
7
8
double S = Math.cbrt(10), small = 1000d, aa = 0, bb = 0;
for(double a = 1; a <= 1000; a++)
    for(double b = 1; b <= 1000; b++)
        if(Math.abs((a/b) - S) < small){
            small = Math.abs((a/b) - S);
            aa = a; bb = b;
        }
System.out.println(aa + bb);

Bill Bell
Apr 6, 2015

One need not search the entire space of (a,b)'s. Indeed, the following code could be tightened. The variable 'k' is present because I was checking the code for smaller values of it.

sir, can u suggest a code for netbeans??

Aditya Kumar - 6 years, 1 month ago

Log in to reply

NetBeans is a development platform--see the wikipedia article. So I assume you mean code for Java. I'm afraid I haven't written anything in that language for twenty years or so.

Bill Bell - 6 years, 1 month ago

Log in to reply

Its ok sir. Thanks!

Aditya Kumar - 6 years ago
Kartik Sharma
Mar 29, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
>>> import itertools
>>> from decimal import *
>>> A = {}
>>> for a in range(1,1000):
    for b in range(1,1000):
        P = Decimal(a)/Decimal(b)
        S = 10**(Decimal(1)/Decimal(3))
        C = abs(P - S)
        A[(a,b)] = C
>>> min_val = min(A.itervalues())
>>> [k for k, v in A.iteritems() if v==min_val]
[(851, 395)]

Tarun Singh
Mar 19, 2015
1
2
3
4
5
6
s=2.15443469003
for i in xrange(1,1000):
    for l in xrange(1,1000):
        a=float(i)/float(l)
        if round(a,5)==round(s,5):
            print 'Answer : ', i+l

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...