forex software

Create and Test Forex Strategies

forex software

Skip to forum content

Forex Software

Create and Test Forex Strategies

You are not logged in. Please login or register.


Forex Software → Expert Advisor Studio → New Year Challenge! Win EA Studio or FSB Pro license!

Pages 1

You must login or register to post a reply

RSS topic feed

Posts: 11

Topic: New Year Challenge! Win EA Studio or FSB Pro license!

Hello Dear Traders,

Merry Christmas to all of you and your families! Success and health in your new year 2020!

I'm glad to present a little challenge for you. It is a short programming task that can be solved in a few lines of code.

The solution requires out of the box thinking and I'll be very happy if some of you manage to solve it.

I always try to support creative and determined people and will be please to me to reward the winners with free EA Studio or FSB Pro lifetime license.

Here is the challenge.

Write a single function called add, which takes either a single numeric argument or zero arguments. When it is called in a row as follows, it must return the sum of the arguments:

function add(n) {
   // Write your code here
  ...
}

console.log( add()             ); // =>  0
console.log( add(1)()          ); // =>  1
console.log( add(1)(2)()       ); // =>  3
console.log( add(1)(2)(3)()    ); // =>  6
console.log( add(1)(2)(3)(4)() ); // => 10

You may use any programming language. You may not write code out of the add function or use global variables.

I expect your answers by the end of 2019.

Happy hacking!

Re: New Year Challenge! Win EA Studio or FSB Pro license!

Miroslav - haw about the following in function in FORTRAN?

       FUNCTION ADD(N)
       INTEGER ADD
       ADD = 0
       DO i = 0,N
         ADD = ADD + i
       ENDDO
       END

Re: New Year Challenge! Win EA Studio or FSB Pro license!

@gcm it may not work.

..

We already have one correct answer sent to PM. I'm very happy from that!!!

Please keep trying and send your answer to me via PM or via email.

Please post them in the public topic on January 1st.

Happy hacking!

4 (edited by gary199233 2019-12-30 13:04:46)

Re: New Year Challenge! Win EA Studio or FSB Pro license!

How about this? I use python

class CustomInt(int):
    def __call__(self, v=0):
        return CustomInt(self + v)

def add(v = 0):
    return CustomInt(v)

Re: New Year Challenge! Win EA Studio or FSB Pro license!

Hello Gary,

The code must be entirely in the `add` function.

6 (edited by GD 2020-01-01 07:44:43)

Re: New Year Challenge! Win EA Studio or FSB Pro license!

It is very simple.

I will present you the difficult part, the algorithm!

Xn= SumXk+(n-1)
k=1 to n-1
X1=0
n=1 to inf

Xn=X(n)

For the people who have some degree in mathematics, it is a kind of programming language.
Can somebody give me a graph? X vs n? Then we have Functional Mathematics...
Matlab is fine! Now you can play...

HAPPY NEW YEAR BOYS AND GIRLS

Re: New Year Challenge! Win EA Studio or FSB Pro license!

Happy New Year!

We have only one correct solution to the challenge sent to me buy @rjectweb. Congratulations!


Here is the answer:

function add(n) {
    function adder(m) {
        return m === undefined
            ? n
            : add(n + m);
    }

    return n === undefined
        ? 0
        : adder;
}

Example:
add(1)(1)(1)(0)(0)(0)(1)(1)(1)() === 6; // SOS :)

It works with any number of calls.

The problem can be solved also with anonymous functions and a single expression, but it is not as readable as the previous one:

const add = n => 
    n === undefined
        ? 0
        : m => m === undefined
                  ? n
                  : add(n + m);
Post's attachments

new-year-chalange-2020-lambda.png
new-year-chalange-2020-lambda.png 8.69 kb, file has never been downloaded. 

You don't have the permssions to download the attachments of this post.

8 (edited by GD 2020-01-01 08:12:09)

Re: New Year Challenge! Win EA Studio or FSB Pro license!

And some GOOD NEWS for EA Studio

I measured CPU utlization in Win 10 x64. I have 4x2 cores

Firefox x64  is 20%
Chrome x64 is 30%
The new Chromiun Edge by Microsoft is 40%. The last one is a very good result!
You can have two FAST runs at the same time against 5 SLOW runs!

I think EA studio can be used also to measure the speed of a browser!!!

HAPPY NEW YEAR

Re: New Year Challenge! Win EA Studio or FSB Pro license!

Let's explain how the `add` function works.

As I learned from my students years when we have a math problem, we have to search for clues for the solution in the given conditions.

Let's start with the first example:

add() returns 0

The function add can be called with one numeric argument or without arguments. In JavaScript, we check if an argument is given by comparing it to `undefined`.

function add(n) {
    if (n === undefined) {
        return 0;
    }
}

add(); // => 0

Ok, it was easy.

From the second example `add(1)() = 1`, we see that `add` must return a function in order to be able to call it. let's call the returned function `adder`:


function add(n) {
    if (n === undefined) {
        return 0;
    }

    function adder(m) {

    }

     return adder;
}

The `adder` also takes one or zero arguments. When it is called without an argument, it must return the number given initially to `add`:


function add(n) {
    if (n === undefined) {
        return 0;
    }

    function adder(m) {
        if (m === undefined) {
            return n;
        }
    }

     return adder;
}

add(7)(); // => 7

Now that example works ` add(7)() => 7 `. We called that "closure". It means that when a function ( `add` ) returns another function ( `adder` ), the returned function "remembers" the arguments from the scope of the outer function. We use that to be able to keep the current sum between the calls. So, `adder` remembers `n` and it is able to return it.

Only one thing remained - to be able to call `add` in a chain like that: `add(n1)(n2)(n3)...(nt)() => n1+n2+n3+ ... + nt;`

We use recursion for that. `adder` calls `add` with the sum of `n` and `m`.

function add(n) {
    if (n === undefined) {
        return 0;
    }

    function adder(m) {
        if (m === undefined) {
            return n;
        }

        return add(n + m);
    }

     return adder;
}

add(7)(0)(7)(); // => 14

It appears that when we call `add` with a number, it returns `adder` as many times as we continue giving a numeric argument. We show the result when calling without an argument and then `adder` returns the current sum equal to `n`.

So we used 'closure' and 'recursion' in that challenge to solve a task, which looks unsolvable. It is a usual example of 'closure' to use two separate functions like that:

function makeAdder(n) {
   function adder(m) {
       return m + n;
   } 
   return adder;
}

const adder5 = makeAdder(5);

adder5(7); // => 12

However, this uses two separate functions, it is limited to adding only two numbers, and the second function can only add 5.

My challenge was to make it with one function, that is able to sum an arbitrary count of numbers and also to be able to be "reset". It is almost magic! I was not sure if it was possible at all.

Re: New Year Challenge! Win EA Studio or FSB Pro license!

Of course, this challenge has only an academical purpose.

If we want to make a function that sums an arbitrary count of numbers, it is much effective to do it like that:

function sumAll() {
    let sum = 0;

    for (let i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }

    return sum;
}

sumAll(3, 5, 7); // => 15

But where are the art and fun?

Happy new year!!!

Re: New Year Challenge! Win EA Studio or FSB Pro license!

Hello and happy New Year to all of you!

I'm very happy to have found a valid solution to the challenge. I was on a trip to Valencia to visit my wife's friends and when I saw the post about the New Year Challenge I only had my mobile and an online javascript editor (js.do) to test the codes. I learnt online about "currying" (I didn't know such a thing existed) and tested some solutions with functions defining and calling functions inside. Finally handling the exceptions (the no arguments usage of the function) with "undefined" was the last part of my try. It is great to have found a valid solution!

Last year I had my first son and I've been quite busy since then. For that reason I don't write too much in the forum, but I visit it nearly everyday, eagerly waiting for news and improvements of such a great software that it is EA Studio. Thanks a lot, Popov, for having created it and my best wishes for you and your team for the new year!

Best regards!

Posts: 11

Pages 1

You must login or register to post a reply

Forex Software → Expert Advisor Studio → New Year Challenge! Win EA Studio or FSB Pro license!

Similar topics in this forum