Crazy math problems.

Discussion in 'Public General Chat' started by Arimil, Dec 13, 2010.

  1. Arimil
    Veteran Admin

    Joined:
    Apr 26, 2010
    Messages:
    2,044
    Likes Received:
    1
    Looks like Java... I hate Java. :p
     
  2. Fikusan
    Veteran Final Fantasy Member

    Joined:
    Jun 24, 2008
    Messages:
    268
    Likes Received:
    1
    its actually a mutated C that runs in a java based compiler

    I brute forced #9 in 1.1865 seconds

    Code:
    A Pythagorean triplet is a set of three natural numbers, a  b  c, for which,
    
    a2 + b2 = c2
    For example, 32 + 42 = 9 + 16 = 25 = 52.
    
    There exists exactly one Pythagorean triplet for which a + b + c = 1000.
    Find the product abc.
    Code:
    function [ansA,ansB,ansC] = PythagoreanTriplet()
    for c = 1000:-1:1
        for b = c:-1:1
            for a = b:-1:1
                if a + b + c == 1000
                    if a^2 + b^2 == c^2
                        ansA = a;
                        ansB = b;
                        ansC = c;
                        return;
                    end
                end
            end
        end
    end
    ans =
    31875000 (200*375*425)
     
    Last edited: Dec 31, 2010
  3. Fikusan
    Veteran Final Fantasy Member

    Joined:
    Jun 24, 2008
    Messages:
    268
    Likes Received:
    1
    I figured out #18 in 0.350815 seconds:
    Code:
    By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
    
    [COLOR="Red"]3[/COLOR]
    [COLOR="red"]7[/COLOR] 4
    2 [COLOR="red"]4[/COLOR] 6
    8 5 [COLOR="red"]9[/COLOR] 3
    
    That is, 3 + 7 + 4 + 9 = 23.
    
    Find the maximum total from top to bottom of the triangle below:
    
    75
    95 64
    17 47 82
    18 35 87 10
    20 04 82 47 65
    19 01 23 75 03 34
    88 02 77 73 07 63 67
    99 65 04 28 06 16 70 92
    41 41 26 56 83 40 80 70 33
    41 48 72 33 47 32 37 16 94 29
    53 71 44 65 25 43 91 52 97 51 14
    70 11 33 28 77 73 17 78 39 68 17 57
    91 71 52 38 17 14 91 43 58 50 27 29 48
    63 66 04 68 89 53 67 30 73 16 69 87 40 31
    04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
    using the code:
    Code:
    [SPOILER]function pathSum = MaxTreePath(Tree, node, depth)%start with node = 1; depth = 1
    if node > numel(Tree)
        pathSum = 0;
    else
        pathSum = max(MaxTreePath(Tree,depth+node,depth+1),MaxTreePath(Tree,depth+1+node,depth+1)) + Tree(node);
    end
    end[/SPOILER]
    and sending it an array of all of the elements in the triangle (read left right, top down)

    This was a quick dynamic programming approach that I learned in my algorithm's class last quarter, its efficient, but it does check every path

    Edit: i figured out a more efficient algorithm that runs in O(n) time and only calculates the length of the last row paths, ill code it up tomorrow and see if it works on #67


    Edit #2:
    did #18 in 0.001072 seconds and #67 in 0.002109 seconds using the following code:
    Code:
    [SPOILER]function pathSum = MaxTreePath(Tree)
    dim = size(Tree,2);
    for i = (dim-1):-1:1
        for j = 1:i
            Tree(i,j) = Tree(i,j) + max(Tree(i+1,j),Tree(i+1,j+1));
        end
    end
    pathSum = Tree(1);
    end[/SPOILER]
    where i had the tree stored in a matrix (which better fits the input given to me)
     
    Last edited: Jan 1, 2011