Interesting math problem

in #mathematics7 years ago

This is easy to understand math problem which i solved using C++.

Task

Make equations for as many natural numbers (including zero) using only numbers 1, 2, 3, 4, 5 and operators +, -, *, / exactly once. Example: 14 = 5 * 3 - 4 / 2 + 1

I encourage you to try it yourself. :D

Solution (spoiler alert!)

After thinking about it for few days I was able to find only solutions for 19 different numbers. I was told there were more solutions, so programmed C++ program to test it. I got the results and I was correct, there are only solutions for 19 different numbers. Solutions made with my program are listed here:

NumberOne of solutions
05 / 1 - 4 * 2 + 3
1No solution
24 / 2 * 3 - 5 + 1
35 / 1 - 3 * 2 + 4
45 * 1 - 3 + 4 / 2
54 / 1 - 5 + 3 * 2
65 * 1 - 4 / 2 + 3
75 / 1 - 4 + 3 * 2
85 / 2 * 4 - 3 + 1
95 / 1 * 2 - 4 + 3
105 / 1 - 3 + 4 * 2
115 / 1 * 2 - 3 + 4
125 / 2 * 4 - 1 + 3
135 / 1 * 3 - 4 + 2
145 * 3 - 4 / 2 + 1
155 / 1 - 2 + 4 * 3
165 * 3 - 1 + 4 / 2
175 / 1 * 3 - 2 + 4
18No solution
195 / 1 * 4 - 3 + 2
20No solution
215 / 1 * 4 - 2 + 3

Code

I made this code to work not to look pretty or to be compatible with other tasks, so bare with me this code is messy.

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>

using namespace std;

double calculate(string s) {
    double br[5];
    char op[4];
    br[0] = s[0] - '0';
    br[1] = s[2] - '0';
    br[2] = s[4] - '0';
    br[3] = s[6] - '0';
    br[4] = s[8] - '0';

    op[0] = s[1];
    op[1] = s[3];
    op[2] = s[5];
    op[3] = s[7];

    int av[5];
    for(int i = 0; i < 5; i++) av[i] = 1;

    for(int i = 0; i < 4; i++) {
        if(op[i] == '/') {
            br[i] = br[i] / br[i+1];
            av[i + 1] = 0;
            op[i] = 'd';
            break;
        }
        else if(op[i] == '*') {
            br[i] = br[i] * br[i+1];
            av[i + 1] = 0;
            op[i] = 'd';
            break;
        }
    }
    for(int i = 0; i < 4; i++) {
        if(op[i] == '/') {
            int fi, se;
            for(int j = i; j >= 0; j--) {
                if(av[j]) {
                    fi = j;
                    break;
                }
            }
            for(int j = i+1; j < 5; j++) {
                if(av[j]) {
                    se = j;
                    break;
                }
            }
            br[fi] = br[fi] / br[se];
            av[se] = 0;
            op[i] = 'd';
            break;
        }
        else if(op[i] == '*') {
            int fi, se;
            for(int j = i; j >= 0; j--) {
                if(av[j]) {
                    fi = j;
                    break;
                }
            }
            for(int j = i+1; j < 5; j++) {
                if(av[j]) {
                    se = j;
                    break;
                }
            }
            br[fi] = br[fi] * br[se];
            av[se] = 0;
            op[i] = 'd';
            break;
        }
    }
    for(int i = 0; i < 4; i++) {
        if(op[i] == '+') {
            int fi, se;
            for(int j = i; j >= 0; j--) {
                if(av[j]) {
                    fi = j;
                    break;
                }
            }
            for(int j = i+1; j < 5; j++) {
                if(av[j]) {
                    se = j;
                    break;
                }
            }
            br[fi] = br[fi] + br[se];
            av[se] = 0;
            op[i] = 'd';
            break;
        }
        else if(op[i] == '-') {
            int fi, se;
            for(int j = i; j >= 0; j--) {
                if(av[j]) {
                    fi = j;
                    break;
                }
            }
            for(int j = i+1; j < 5; j++) {
                if(av[j]) {
                    se = j;
                    break;
                }
            }
            br[fi] = br[fi] - br[se];
            av[se] = 0;
            op[i] = 'd';
            break;
        }
    }
    for(int i = 0; i < 4; i++) {
        if(op[i] == '+') {
            int fi, se;
            for(int j = i; j >= 0; j--) {
                if(av[j]) {
                    fi = j;
                    break;
                }
            }
            for(int j = i+1; j < 5; j++) {
                if(av[j]) {
                    se = j;
                    break;
                }
            }
            br[fi] = br[fi] + br[se];
            av[se] = 0;
            op[i] = 'd';
            break;
        }
        else if(op[i] == '-') {
            int fi, se;
            for(int j = i; j >= 0; j--) {
                if(av[j]) {
                    fi = j;
                    break;
                }
            }
            for(int j = i+1; j < 5; j++) {
                if(av[j]) {
                    se = j;
                    break;
                }
            }
            br[fi] = br[fi] - br[se];
            av[se] = 0;
            op[i] = 'd';
            break;
        }
    }
    return br[0];
}

string f[100];
int has[100];

int main() {

    for(int i1 = 1; i1 <= 5; i1++) {
        for(int j1 = 1; j1 <= 4; j1++) {
            for(int i2 = 1; i2 <= 5; i2++) {
                for(int j2 = 1; j2 <= 4; j2++) {
                    for(int i3 = 1; i3 <= 5; i3++) {
                        for(int j3 = 1; j3 <= 4; j3++) {
                            for(int i4 = 1; i4 <= 5; i4++) {
                                for(int j4 = 1; j4 <= 4; j4++) {
                                    for(int i5 = 1; i5 <= 5; i5++) {
                                        if(i1 == i2 || i1 == i3 || i1 == i4 || i1 == i5
|| i2 == i3 || i2 == i4 || i2 == i5 || i3 == i4 || i3 == i5 || i4 == i5) continue;
                                        if(j1 == j2 || j1 == j3 || j1 == j4 || j2 == j3
|| j2 == j4 || j3 == j4) continue;
                                        string s;
                                        s.push_back(i1 + '0');
                                        if(j1 == 1) s.push_back('+');
                                        else if(j1 == 2) s.push_back('-');
                                        else if(j1 == 3) s.push_back('*');
                                        else if(j1 == 4) s.push_back('/');
                                        s.push_back(i2 + '0');
                                        if(j2 == 1) s.push_back('+');
                                        else if(j2 == 2) s.push_back('-');
                                        else if(j2 == 3) s.push_back('*');
                                        else if(j2 == 4) s.push_back('/');
                                        s.push_back(i3 + '0');
                                        if(j3 == 1) s.push_back('+');
                                        else if(j3 == 2) s.push_back('-');
                                        else if(j3 == 3) s.push_back('*');
                                        else if(j3 == 4) s.push_back('/');
                                        s.push_back(i4 + '0');
                                        if(j4 == 1) s.push_back('+');
                                        else if(j4 == 2) s.push_back('-');
                                        else if(j4 == 3) s.push_back('*');
                                        else if(j4 == 4) s.push_back('/');
                                        s.push_back(i5 + '0');
                                        double rez = calculate(s);
                                        if(rez < 0) continue;
                                        if(rez == trunc(rez)) {
                                            f[(int)rez] = s;
                                            has[(int)rez] = 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    for(int i = 0; i < 50; i++) {
        if(has[i]) {
            cout << i << " = " << f[i] << endl;
        }
        else cout << i << " = ?" << endl;
    }
    system("pause");
    return 0;
}
Sort:  

If you love this kind of math, you will really enjoy solving Euler problems on projecteuler. They are really interesting and have a lot to do with prime numbers. Have you ever heard of it?

No I havent, thank you for the recommendation :D

Congratulations @worly! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You made your First Comment

Click on any badge to view your own Board of Honnor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

If you want to support the SteemitBoard project, your upvote for this notification is welcome!