Search code examples
mathmathematical-optimization

What is the equation to generate `5,3,1,3,5` in sequence?


Yesterday, I did a pseudo code test to get the boot camp's scholarship, 5 questions in 1 hour, I failed to answer 2 out of 5. I could not find the answer using math equation.

// === === === question 3
// Output:
// *****
//   ***
//     *
//   ***
// *****

// === === === question 5
// Output:
// *   *
//  * *
//   *
//  * *
// *   *

tl:dr

This is the answer in JavaScript and does not use math equation. I am not a mathematician nor a software engineer. I am actually new to software engineering, if you have more straight forward answer, please let me know.

I assume the output as blocks / air + asterisk.

question 3

// v2 air + asterisk

function cv2() {
    for (i=5; i > 0; i--) {
        // i = [5,3,1]
        if (i %2 == 1) {
            var air = ' '.repeat(5-i);
            var asterisk = '*'.repeat(i);
            console.log(air+asterisk);
        }
    }
    for (i=3; i <= 5; i++) {
        // i = [3,5];
        if (i %2 == 1) {
            var air = ' '.repeat(5-i);
            var asterisk = '*'.repeat(i);
            console.log(air+asterisk);
        }
    }
}

cv2();

question 5

// v1

function ev1() {
    var i = 0;
    var j = 4;
    for (row = 0; row < 5; row++) {
        var result = '';
        for (col = 0; col < 5; col++) {
            if (row == i && col == j) {
                result += '*';
                i++;
                j--;
            } else if (row == col) {
                result += '*';
            } else {
                result += ' ';
            }
        }
        console.log(result);
    }
}

//ev1()

answer with equation

// question 3
// using equation provided by @lukeg in stackoverflow.
// v4
// x = [0,1,2,3,4]
// asterisk f(x) = |-4 + 2 * x | + 1
// air f(x) = | | -4 + 2 * x | - 4 |

function cv4() {
    for (x = 0; x < 5; x++) {
        var air = Math.abs(Math.abs(-4 + 2 * x) - 4);
        var asterisk = Math.abs(-4 + 2 * x) + 1;
        console.log(' '.repeat(air) + '*'.repeat(asterisk));
    }
}

//cv4();

// v5 == failed
// asterisk f(x) = 2(x)^3 / 3 - 2(x)^2 - 2x/3 + 5

function cv5() {
    for (x = 0; x < 5; x++) {
        var asterisk = 2 * Math.pow(x,3) / 3 - 2 * Math.pow(x,2) - 2 * x / 3 + 5;
        console.log('*'.repeat(asterisk));
    }
}

//cv5()

// v6 == failed
// x = [0,1,2,3,4]
// y = [5,3,1,3,5]
// asterisk f(x) = -(x)^4 / 3 + 8(x)^3 / 3 - 17(x)^2 / 3 + 4x/3 + 5

function cv6() {
    for (x = 0; x < 5; x++) {
        var asterisk = -Math.pow(x,4) / 3 + 8 * Math.pow(x,3) / 3 - 17 * Math.pow(x,2) / 3 + 4 * x / 3 + 5;
        console.log('*'.repeat(asterisk));
    }
}

//cv6()

// v7
// equation in vertex form
// y = a | x - h | + k , vertex (h, k), another point on the right of the vertex (x2, y2);
// a = y2 - k / x2 - h
// asterisk f(x) = 2 | x - 2 | + 1 
// air f(x) = -2 | x - 2 | + 4
// x = [0,1,2,3,4] y = [5,3,1,3,5]

function cv7() {
    for (x = 0; x < 5; x++) {
        var air =  -2 * Math.abs(x - 2) + 4;
        var asterisk = 2 * Math.abs(x - 2) + 1;
        console.log(' '.repeat(air) + '*'.repeat(asterisk));
    }
}

cv7();

edit: change in pure math to using equation edit: add lukeg equation, check function cv4(). edit: try to solve using Raymond Chen's technique, check function cv5() and cv6(). so far, failed.


Solution

  • If I understand the question, you are looking for a concise formula that generates the sequence 5,3,1,3,5? Does f(x) = |-4 + 2 * x| + 1, for x in (0,1,2,3,4) satisfy your needs?

    As as a side note, I don't think the task has a lot to do with programming, and more with 'aha' moment.