Page 5 of 5 First 12345
  1. #81
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Re: 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 by AceInfinity; 06-30-2013 at 02:13 AM.
    Automation Programmer
    Microsoft MVP [2012 - 2018]


    • Ad Bot

      advertising
      Beep.

        
       

  2. #82
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Re: Project Euler

    Here's my solution to #1 in Haskell :)
    Code:
    sum [x | x <- [3..999], x `mod` 3 == 0 || x `mod` 5 == 0]
    Automation Programmer
    Microsoft MVP [2012 - 2018]

  3. #83
    Tekno Venus's Avatar
    Join Date
    Jul 2012
    Location
    UK
    Age
    20
    Posts
    5,943
    • specs System Specs
      • Manufacturer:
        Custom Built
      • Motherboard:
        ASUS Z170I ITX
      • CPU:
        Intel Core i7 6700K
      • Memory:
        16GB DDR4
      • Hard Drives:
        500GB Samsung 850 EVO, 2TB Seagate HDD
      • Power Supply:
        450W Corsair SFX
      • Case:
        Silverstone SG13 ITX
      • Cooling:
        Corsair H60i
      • Display:
        Dell U2715H - 2160x1440 27 inch
      • Operating System:
        Windows 10 Pro x64

    Re: Project Euler

    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


  4. #84
    x BlueRobot's Avatar
    Join Date
    May 2013
    Location
    Minkowski Space
    Posts
    1,872

    Re: Project Euler

    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?
    Machines Can Think

    We don't make mistakes; we just have happy accidents.

  5. #85
    tom982's Avatar
    Join Date
    May 2012
    Location
    Southampton, England
    Age
    24
    Posts
    4,328
    • specs System Specs
      • Manufacturer:
        Custom
      • Model Number:
        Custom
      • Motherboard:
        ASUS P8Z77-V PRO
      • CPU:
        Intel Core i7 3770K @4.5GHz
      • Memory:
        Corsair Vengeance 2x4GB 1600MHz LP - White
      • Graphics:
        Gigabyte HD 7850 2GB @1050MHz
      • Sound Card:
        Stock
      • Hard Drives:
        Seagate Barracuda 2TB 7200rpm [Internal], 2x500GB Seagate FreeAgent (External)
      • Disk Drives:
      • Power Supply:
        Corsair TX650W V2 80+ Bronze
      • Case:
        NZXT Phantom 410 White
      • Cooling:
        Corsair H100 CPU Water Cooler, 1x140mm stock fan and 1x120mm stock fan
      • Display:
        LG 23&quot; IPS Monitor (1920*1080)
      • Operating System:
        Windows 10

    Re: Project Euler

    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();
            }
        }
    }
    writhziden and x BlueRobot say thanks for this.
    Having problems with SFC or Windows Update? Start a new thread in the Windows Update forum!

    Haven't heard from us in a while? Post Here to remind us.

    Have we helped you? Help us help others and Donate!

  6. #86
    x BlueRobot's Avatar
    Join Date
    May 2013
    Location
    Minkowski Space
    Posts
    1,872

    Re: Project Euler

    Thanks Tom, that works perfectly!
    Machines Can Think

    We don't make mistakes; we just have happy accidents.

  7. #87
    tom982's Avatar
    Join Date
    May 2012
    Location
    Southampton, England
    Age
    24
    Posts
    4,328
    • specs System Specs
      • Manufacturer:
        Custom
      • Model Number:
        Custom
      • Motherboard:
        ASUS P8Z77-V PRO
      • CPU:
        Intel Core i7 3770K @4.5GHz
      • Memory:
        Corsair Vengeance 2x4GB 1600MHz LP - White
      • Graphics:
        Gigabyte HD 7850 2GB @1050MHz
      • Sound Card:
        Stock
      • Hard Drives:
        Seagate Barracuda 2TB 7200rpm [Internal], 2x500GB Seagate FreeAgent (External)
      • Disk Drives:
      • Power Supply:
        Corsair TX650W V2 80+ Bronze
      • Case:
        NZXT Phantom 410 White
      • Cooling:
        Corsair H100 CPU Water Cooler, 1x140mm stock fan and 1x120mm stock fan
      • Display:
        LG 23&quot; IPS Monitor (1920*1080)
      • Operating System:
        Windows 10

    Re: Project Euler

    No problem! Good to see you back here.
    Having problems with SFC or Windows Update? Start a new thread in the Windows Update forum!

    Haven't heard from us in a while? Post Here to remind us.

    Have we helped you? Help us help others and Donate!

  8. #88
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Re: Project Euler

    Quote Originally Posted by tom982 View Post
    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.
    Automation Programmer
    Microsoft MVP [2012 - 2018]

Page 5 of 5 First 12345

Similar Threads

  1. [Project] StringCrypt - Work in Progress
    By AceInfinity in forum Programming
    Replies: 1
    Last Post: 10-01-2012, 11:38 PM
  2. Project Ideas
    By HonorGamer in forum Programming
    Replies: 15
    Last Post: 07-29-2012, 05:08 PM
  3. VB Project... To help me learn
    By GZ in forum Programming
    Replies: 7
    Last Post: 06-02-2012, 10:57 PM

Log in

Log in