how to get atleast 50 points in eulersum? what is wrong in my approach? how to handle precission loss with Big-Integers?

i got AC in 2 subcases of TASK-1,i think this is due to precission loss,anything wrong with my approach?

my code->

class eulernumber
{

public static void main(String[] args)

{
    Scanner sc=new Scanner(System.in);

    BigInteger n=new BigInteger("0");

    n=sc.nextBigInteger();        

long ii=2;

    BigDecimal e=new BigDecimal("2.718281");

BigDecimal i=new BigDecimal(n);

    BigInteger l=BigInteger.ZERO;

   BigDecimal ll=e.add(i.subtract(BigDecimal.ONE).multiply(e));//find nth term of AP

l=e.add(ll).toBigInteger(); fing sum of nth and first term

BigDecimal bb=new BigDecimal(l);

   System.out.println(i.divide(BigDecimal.valueOf(ii)).multiply(bb).toBigInteger()); //find sum of nth term of AP
     
} 

}

Ofcourse ,it’s a precision loss.For 50 points too, u need atleast 100 decimal places precision for e. So,this is not the right approach.
Best approach is using Beatty Sequence.
Let editorials come.

1 Like

Hello! refer this link it gives a good generalization for irrational numbers not just e!

1 Like

Initially even I thought It was an AP but I realised that it isn’t , after generating the differences .

| 3 3 | 3 3 3 | 3 3 | 3 3 3 | 3 3 | 3 3 3 | 3 3 | 3 3 3 | 3 3 | 3 3 3 | 3 3 3 | 3 3 | 3 3 3 | 3 3 |
Here ‘|’ denotes a difference of 2.

This is how the differences look , you can see that its some non-trivial pattern.
So using the properties of Beatty sequences makes the job easier.

1 Like

Read the first 3 pages of this paper and continued fractions for 100 points. Little bit involving but very satisfying. The method involves no floating point division and multiplication which was the heart of the question.

3 Likes

but approach is right? means logic is right? except proper implementation? can i consider the sequence as AP?

It’s not an AP

why? how i got AC in 2 testcases??

Google Beatty Sequence.Its a sequence of floor of multiples of an irrational number.
Not simple AP,or GP,etc.

Thanks a lot,but why i got AC?

Maybe that’s just pure luck. But for me, I didn’t get 50 pts even with 100 places of precision. I had to use 4000 places of precision for 50 points.

@vivek96 The two testcases may be some base cases for which your output is correct…Important thing is It is not an AP and your Approach is wrong you will not get 50 points with proper implementation too.

1 Like

how to overcome precission loss? @ista2000

@vivek96 I used the decimal module of python :slight_smile:

unable to understand