Project Euler

It was a little, but mostly coming up with a fast way of doing it, and for me, I tried several ways before I decided to see how I could map the digits to specific numbers. I was working on #60 when I gave up on that one, and decided to try #61 while I "waited."

I'm quite happy to see this thread getting some attention though. That is really cool.
 
Last edited:
I've done #9 using an inefficient brute force method. I am looking at how to improve it atm...

Code:
Imports System.Math
Imports System.Threading


Module Module1
    Public Function Ccalc(ByVal a, ByVal b) As Decimal
        Return Sqrt(a ^ 2 + b ^ 2)
    End Function

    Sub Main()
        Dim a As Integer = 3
        Dim b As Integer = 4
        Dim c As Decimal
        Dim answer As Long
        Dim done As Boolean

        For i As Integer = 1 To 500
            a += 1
            For j As Integer = 1 To 500
                b += 1
                c = Ccalc(a, b)
                If a + b + c = 1000 AndAlso a < b AndAlso b < c AndAlso c / 1 = c \ 1 Then
                    Console.WriteLine("a = {0}, b = {1}, c = {2}, sum = {3}", a, b, c, a + b + c)
                    done = True
                    answer = a * b * c
                    Exit For
                End If
                Console.WriteLine("a = {0}, b = {1}, c = {2}, sum = {3}", a, b, c, a + b + c)
            Next
            b = 4
            If done = True Then Exit For
        Next
        Console.WriteLine(answer)
        Console.ReadLine()
    End Sub

End Module
 
My solution for Problem 8 doesn't appear to be working correctly, it appears that I might have a overflow error despite a long being used to store the result?

Code:
static void Main(string[] args)
        {

            long current_product, max_product = 0;
            String number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
            char[] char_digits = number.ToCharArray();
            int[] digits = new int[1000];

            for(int i=0; i < char_digits.Length; i++) {digits[i] = (int)Char.GetNumericValue(char_digits[i]);}

            for(int i=0; i <= digits.Length - 13; i++)
            {current_product = digits[i] * digits[i + 1] * digits[i + 2] * digits[i + 3] * digits[i + 4] * digits[i + 5] * digits[i + 6] * digits[i + 7] * digits[i + 8] * digits[i + 9] * digits[i + 10] * digits[i + 11] * digits[i + 12];

                if (current_product > max_product) { max_product = current_product;}
            }

            Console.WriteLine(max_product);
            Console.ReadKey();

            
        }

The code appears to work for four adjacent digits?

Code:
static void Main(string[] args)
        {

            long current_product, max_product = 0;
            String number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
            char[] char_digits = number.ToCharArray();
            int[] digits = new int[1000];

            for(int i=0; i < char_digits.Length; i++) {digits[i] = (int)Char.GetNumericValue(char_digits[i]);}

            for(int i=0; i <= digits.Length - 4; i++)
            {current_product = digits[i] * digits[i + 1] * digits[i + 2] * digits[i + 3];

                if (current_product > max_product) { max_product = current_product;}
            }

            Console.WriteLine(max_product);
            Console.ReadKey();

            
        }

Any suggestions?
 
Your problem is storing the digits as ints, change it to long and it'll return the correct number. You're right to suspect an overflow and it happens here:

Code:
current_product = digits[i] * digits[i + 1] * digits[i + 2] * digits[i + 3] * digits[i + 4] * digits[i + 5] * digits[i + 6] * digits[i + 7] * digits[i + 8] * digits[i + 9] * digits[i + 10] * digits[i + 11] * digits[i + 12];
This is equivalent to:

Code:
int a = digits[i];
int b = digits[i+1];
int c = a*b;
int d = digits[i+2];
int e = c*d;
...
int z = x*y;
current_product = (long)z;

Which obviously overflows.

Code:
using System;


namespace ProjectEuler
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            long currentProduct, maxProduct = 0;
            const string number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
            char[] charDigits = number.ToCharArray();
            long[] digits = new long[1000];


            for (int i = 0; i < charDigits.Length; i++)
            {
                digits[i] = (long)char.GetNumericValue(charDigits[i]);
            }


            for (int i = 0; i <= digits.Length - 13; i++)
            {
                currentProduct = digits[i] * digits[i + 1] * digits[i + 2] * digits[i + 3] * digits[i + 4] * digits[i + 5] * digits[i + 6] * digits[i + 7] * digits[i + 8] * digits[i + 9] * digits[i + 10] * digits[i + 11] * digits[i + 12];


                if (currentProduct > maxProduct)
                {
                    maxProduct = currentProduct;
                }
            }


            Console.WriteLine(maxProduct);
            Console.ReadKey();
        }
    }
}
 
Your problem is storing the digits as ints, change it to long and it'll return the correct number. You're right to suspect an overflow and it happens here:

Code:
current_product = digits[i] * digits[i + 1] * digits[i + 2] * digits[i + 3] * digits[i + 4] * digits[i + 5] * digits[i + 6] * digits[i + 7] * digits[i + 8] * digits[i + 9] * digits[i + 10] * digits[i + 11] * digits[i + 12];
This is equivalent to:

Code:
int a = digits[i];
int b = digits[i+1];
int c = a*b;
int d = digits[i+2];
int e = c*d;
...
int z = x*y;
current_product = (long)z;

Which obviously overflows.

Code:
using System;


namespace ProjectEuler
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            long currentProduct, maxProduct = 0;
            const string number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
            char[] charDigits = number.ToCharArray();
            long[] digits = new long[1000];


            for (int i = 0; i < charDigits.Length; i++)
            {
                digits[i] = (long)char.GetNumericValue(charDigits[i]);
            }


            for (int i = 0; i <= digits.Length - 13; i++)
            {
                currentProduct = digits[i] * digits[i + 1] * digits[i + 2] * digits[i + 3] * digits[i + 4] * digits[i + 5] * digits[i + 6] * digits[i + 7] * digits[i + 8] * digits[i + 9] * digits[i + 10] * digits[i + 11] * digits[i + 12];


                if (currentProduct > maxProduct)
                {
                    maxProduct = currentProduct;
                }
            }


            Console.WriteLine(maxProduct);
            Console.ReadKey();
        }
    }
}

Personally I would keep the string, take substrings, and look at each character as a value (CHARACTER - '0') which will take the ASCII value and convert it to the digit value for your calculations.. You shouldn't need a new array to hold the digits.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top