tags:

views:

338

answers:

4

UPDATE :

This question is not homework. And not waterproof apparantly... I wanted a discussion about internal representation. Of course : the add1000 ought to add 1000.

**Please answer in the spirit of this question... Making this waterproof would make this question longer without no reason.. ** You can beat a pure decimal representation http://stackoverflow.com/questions/1709680/changing-internal-representation-in-runtime UPDATE 2 : see

Create a type that implements this interface :

interface INumber
    {
     void add1000();
     void SetValue(decimal d);
     decimal GetValue();   
    }

so that i iterates as fast as possible from 0 to 10 billion (american billion, so till 10e9) in this for loop :

private static void DoSomeAdding(INumber n)
     {
      Debug.Assert(n.GetValue()==0);

      for (long i=0; i<10000000000; i += 1000)
      {
       n.add1000();
      }

      Debug.Assert(n.GetValue() == 10000000000);

     }

So you can call it as :

DoSomeAdding(new YourNumberClass());
+11  A: 
public Cheating : INumber
{
    static int timesCalled = 0;

    public void add1000() {}
    public void SetValue(decimal d) {}

    public decimal GetValue()
    {
        if (timesCalled == 0)
        {
            timesCalled += 1;
            return 0;
        }

        return 1000000000;
    }
}
Austin Salonen
I suppose one could argue this is not *cheating* but merely an early solution for a test-driven design that passes the one and only test for an interface.
Austin Salonen
I think that defeats the point.
Anton
sigh.........................
Peter
It was clever though.
thismat
+3  A: 
public class JNumber : INumber
{
    decimal num = 0;

    public void add1000()
    {
        num = 10000000000;
    }

    public void SetValue(decimal d)
    {
    }

    decimal GetValue()
    {
        return num;
    }
}

...cheating, but passes.

Justin Niessner
I think that defeats the point.
Anton
+1  A: 

I think you need more requirements. As written the fastest solution would be something like:

class MyNumberClass {
   bool is_ten_billion = false;
   int GetValue() {
      if(is_ten_billion) return 10000000000;
      is_ten_billion = true;
      return 0;
   }

   decimal add1000() {}

   void setValue(decimal d) {}
}

That way the optimizer can dispose of the calls to add1000(), and then of the loop altogether.

David Seiler
I think that defeats the point.
Anton
+6  A: 

Like Anton's solution, but with a bit more care :) Oh, and I've changed the names to be more .NET-like.

public Number : INumber
{
    private decimal value = 0m;
    private int thousands = 0;

    public void Add1000()
    {
        thousands++;
    }

    void SetValue(decimal d)
    {
        value = d;
        thousands = 0;
    }

    decimal GetValue()
    {
        // Careful of the overflow... (do multiplication in decimal)
        value += thousands * 1000m;
        thousands = 0;
        return value;
    }
}
Jon Skeet
I've read the Skeet facts so I will adjust my stopwatch class if your solution doesnot beat mine
Peter
+1 8(But in this case a mixed representation still is a bit faster)
Peter
In fact it's possible that there's a really fast way to convert an integer number of thousands into a decimal by use of a certain amount of cunning... but I'd probably stick with this code.
Jon Skeet