Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
1.  Given a integer N, print the decimal form of 1/2n.
Example:
N=1, print 0.5
N=2, print 0.25
Adding leading/unsignificant zeroes will lead to wrong answer. Example, printing 0.50 instead of 0.5 in above case will lead to wrong answer.


So correct output of 100 will be 0.0000000000000000000000000000007888609052210118054117285652827862296732064351090230047702789306640625
Output for 50 : 0.00000000000000088817841970012523233890533447265625
Output for 200 : 0.0000000000000000000000000000000000000000000000000000000000006223015277861141016250579646224554209682103144689201443821087459887188032139105827696600570131548979236366311772064357280932527780316951302892084253444264507759697835354018025100231170654296875


I am getting output till 28-29 digits after decimal point.Please suggest what can be done to get accurate above mentioned result..

My code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Divideby2
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[t];
            for (int i = 0; i < t; i++)
            {
                a[i] =Convert.ToInt32(Console.ReadLine());
            }
            for (int j = 0; j < t; j++)
            {
               double n = a[j];
               string res = (1 / (Convert.ToDouble(Math.Pow(2, n)))).ToString();
               decimal d = decimal.Parse(res, System.Globalization.NumberStyles.Any);
               Console.WriteLine(d);
            }
            Console.Read();
        }
    }
}
Posted
Comments
Thanks7872 28-May-14 9:47am    
I have never ever seen such a accurate result anywhere in the world.
[no name] 28-May-14 11:09am    
Number of Digits does not mean automatically accuracy....

I think you mean 1 / (2 << n).

Well, this is clearly something you are supposed to work out on your own, but I'll help by telling you two ways you could go about it.

1. Build your own class to represent very large precision numbers. As Stephen suggests, you could use byte arrays but I think a string would make development easier. Implement methods for Math.Pow and division.

2. Using C#, use the built in BigInt type in System.Numerics. This has all the methods you need, but only works with integers. (Hint: Multiply everything by 10 to the power of 200 and stick the point in afterwards).

It takes six lines of code....
 
Share this answer
 
Comments
CPallini 28-May-14 13:17pm    
5.
[no name] 28-May-14 13:33pm    
Straightforward ;) 5.
Member 10397973 29-May-14 8:26am    
I mean 1/(2)^n and not 1/(2<<n)
Please elaborate the logic if possible
Member 10397973 29-May-14 9:18am    
Thanks Rob :-)
Member 10397973 29-May-14 10:14am    
Problem is solved
This task has been set because there isn't a default data type in the .Net framework which will to 200 decimal places.

Sounds like an excellent homework assignment.

My advice would be to use an array of ints. One per position in the decimal.

Effectively you're dividing by two each time. So do that.

Think long division. How do you do long division?

Apply the same process but using an array of integers.
 
Share this answer
 
Comments
Thanks7872 28-May-14 10:10am    
I won't even join the institute which asks 200 digits after decimal :laugh:
Stephen Hewison 28-May-14 10:27am    
I know, but it's a theoretical exercise. Plancks constant only goes to 10^-34 so 200 decimals is more than enough for the quantum world.
Stephen Hewison 28-May-14 10:38am    
Although when working with Pi, 200 digits of precision on not uncommon for precision engineering.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900