HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Custom Big Integer class for Project Euler in C#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
custombigprojectforeulerclassinteger

Problem

I'm trying to teach myself some programming and, working through the Project Euler problems, I've come across some instances where I've needed numbers that are larger than will fit into an int or long variable. I know that existing libraries already accomplish this, but I figured it would be fun/worthwhile/educational to build the tools to handle these myself.

Project Goals:

  • Build a class to add or multiply numbers much larger than int or long.



  • Prioritize cleanliness, extensibility, and ease of integration with other Project Euler solutions.



  • Maximize algorithmic efficiency within these constraints.



Specific Questions:

-
Any feedback on style, structure, convention, etc.

-
Are there any profound improvements I could make to the algorithms.

-
Are there any features or techniques I should research to make this class more full-featured or comprehensive.

I appreciate any feedback, thank you.

```
public class MikesBigInt
{
private List digits;
public int Length { get { return digits.Count; } }
public int this[int index] { get { return digits[index]; } }

public MikesBigInt()
{
digits = new List { 0 };
}
public MikesBigInt(int n)
{
int l = n.ToString().Length;
digits = new List();
for (int i = n.ToString().Length; i-- > 0;) {
digits.Add(n.ToString()[i] - 48);
}
}
public void Disp()
{
int l = digits.Count;
for (int i = digits.Count; i-- > 0;) {
Console.Write(digits[i]);
}
Console.WriteLine();
}
public int ToInt()
{
int res = 0;
for (int i = 0; i neoDigits = new List();

int xL = digits.Count - 1;
int yL = n.Length - 1;

for (int i = 0; i 0;) {
digits.Add(carryTemp.ToString()[i] - 48);
}
Trim();
}

public void Add(MikesBigInt n)
{
int carryTemp = 0;

for (int di = 0; di n.Length) {
for (int di = n

Solution

public MikesBigInt(int n)

There are quite a few improvements you can make here :

public MikesBigInt(int n)
{
    int l = n.ToString().Length;
    digits = new List();
    for (int i = n.ToString().Length; i-- > 0;)
    {
        digits.Add(n.ToString()[i] - 48);
    }
}


-
You are not using the l variable.

-
You are constantly calling .ToString() on a variable that hasn't changed value, since the first call. You can add a helper string variable to improve performance as calling obviously it's better to call .ToString() just once. You are doing this in most of your methods

-
The for loop is really weird, there is a designed place where you change the value of the looping variable (the last segment of the loop), why are you not using that ? + I don't see any reason for the inverse logic here.

-
You should (when possible) specify the length of a list to avoid unnecessary memory allocation. new List(length)

-
Lastly the entire method can be replaced with some LINQ as it wont affect the performance all that much since you are working with ints only which are rather short in digits length :

public MikesBigInt(int n)
{
    digits = n.ToString().Select(c => (int) char.GetNumericValue(c)).ToList();
}


If the inversion logic is necessary for your code to work you can do :

public MikesBigInt(int n)
{
    digits = n.ToString().Select(c => (int)char.GetNumericValue(c)).Reverse().ToList();
}


public int ToInt()

-
This is weird method, I don't think it should be there at all. You should be using your BigInt only when the values are bigger than what int/long can handle, than what's the point of converting it to int ? Odds are that it will overflow the int too.

-
You can use some LINQ here too :

public int ToInt()
{
    return digits.Select((t, i) => t * (int) Math.Pow(10, i)).Sum();
}


Overall design

Arithmetic operations

-
Any operations that you have should return some result instead of saving the value in the current instance of the object. When you add 2 integer values together you get some result right ? Your variable should be able to work as a normal data type.

-
Having that said your operations should be methods in the first place, instead you should override some operators, which can be done like this :

public static MikesBigInt operator +(MikesBigInt first, MikesBigInt second)
{
    // do your logic here
}

Code Snippets

public MikesBigInt(int n)
{
    int l = n.ToString().Length;
    digits = new List<int>();
    for (int i = n.ToString().Length; i-- > 0;)
    {
        digits.Add(n.ToString()[i] - 48);
    }
}
public MikesBigInt(int n)
{
    digits = n.ToString().Select(c => (int) char.GetNumericValue(c)).ToList();
}
public MikesBigInt(int n)
{
    digits = n.ToString().Select(c => (int)char.GetNumericValue(c)).Reverse().ToList();
}
public int ToInt()
{
    return digits.Select((t, i) => t * (int) Math.Pow(10, i)).Sum();
}
public static MikesBigInt operator +(MikesBigInt first, MikesBigInt second)
{
    // do your logic here
}

Context

StackExchange Code Review Q#151912, answer score: 5

Revisions (0)

No revisions yet.