Eggs aren't veg

If every letter corresponds a unique digit in decimal system and T O M + G I V E S = M O S T + T I M E \overline{TOM}+\overline{GIVES} = \overline{MOST}+ \overline{TIME} Find the value of E G G + I S + V E G \overline{EGG}+ \overline{IS} + \overline{VEG}


Details and assumptions :

\bullet If ( A , B , C , D ) = ( 1 , 2 , 3 , 4 ) (A,B,C,D) =(1,2,3,4) , then B A D = 214 \overline{BAD} = 214 .

\bullet Everything is in decimal representation and every letter corresponds to one unique digit. That is why this has exactly one solution.


Everything that i shared on brilliant


Image Credit: Wikimedia Genetic Algorithm by Yearofthedragon


The answer is 1324.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Aditya Raut
Mar 15, 2015

The program will print the solution 2 times, because we are using 8 integers out of 10 so the 2 ! 2! permutations will give the solution 2 times, but anyway, the solution is unique, answer 1324 \boxed{1324}

1
2
3
4
5
6
7
8
9
for i in permutations('0123456789'):
    x=''.join(i)
    [E,G,I,M,O,S,T,V]=list(x)[:8]
    if int(T+O+M)+int(G+I+V+E+S)==int(M+O+S+T)+ int(T+I+M+E) :
        print int(E+G+G)+int(I+S)+int(V+E+G)

1324
1324
>>>

@Raghav Vaidyanathan and @Agnishom Chattopadhyay , my programming friends....

Aditya Raut - 6 years, 3 months ago

"0123456789"="OGSIVMaaET" \text{"0123456789"="OGSIVMaaET"}

Your python code takes 23 seconds to run on my computer.

I actually extended my genetic algorithm from the previous problem to adapt into this. Works well under a second, every time. When I first saw that there were two undefined digits, I thought there might be multiple solutions. My algorithm cannot find both simultaneously. But then I assumed that you must have taken care of that. And I was right. Good job. I have been posting a few programming problems lately, try those if you are interested.

Raghav Vaidyanathan - 6 years, 3 months ago

Log in to reply

That's so amazing, I'm gonna try and learn genetic programming right now ! (Time is not issue for me now, I could've waited for 1 hour if that did give answer :P LOL)

Aditya Raut - 6 years, 3 months ago

This is the most efficient solution so far:

1
2
3
4
5
from itertools import permutations

for t,o,m,g,i,v,e,s in permutations('0123456789',8):
  if int(t+o+m)+int(g+i+v+e+s)==int(m+o+s+t)+int(t+i+m+e):
    print(int(e+g+g)+int(i+s)+int(v+e+g))

1
1324

Using C++ implement the Genetic Algorithm easily after read article from Agnishom Chattopadhyay

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <bits/stdc++.h>

#define POPULATION 100
#define INDEX_MUTATION 3

std::string F = "TOM";
std::string S = "GIVES";
std::string Rx = "MOST";
std::string Ry = "TIME";

typedef struct Chromosome{

    std::string gene;

    int doSearch(char x){
        int ret;
        bool found = false;
        int index = 0;
        while(!found){
            if(gene[index] == x){
                ret = index;
                found = true;
            }
            index++;
        }
        return ret;
    }

    Chromosome() : gene("__EGIMOSTV") {}

    void printGene(){
        std::cout<<gene<<std::endl;
    }

    int geneValue(char x){
        int ret;
        ret = doSearch(x);
        return ret;
    }

    void mutate(){       
            std::random_device rdx;
            std::random_device rdy;
            std::mt19937 genx(rdx());
            std::mt19937 geny(rdy());
            std::uniform_int_distribution<int> rgx(0,9);
            std::uniform_int_distribution<int> rgy(0,9);
            int x, y;

            for(int i=0; i<INDEX_MUTATION; i++){
                x = rgx(genx);
                y = rgy(geny);
                std::swap(gene[x],gene[y]);
            }

    }

} Chromosome;

long fitness(Chromosome c){
    long f = 0, s = 0, rx = 0, ry = 0, r = 0;
    long ret = 0;
    int fsize = F.length();
    int ssize = S.length();
    int rxsize = Rx.length();
    int rysize = Ry.length();

    for(int i=0; i<fsize; i++) {

        long tempf = c.geneValue(F[i]);
        f+=tempf;
        f*=10;

    }

    f/=10;

    for(int i=0; i<ssize; i++) {
        long temps = c.geneValue(S[i]);
        s+=temps;
        s*=10;
    }

    s/=10;

    for(int i=0; i<rxsize; i++) {
        long temprx = c.geneValue(Rx[i]);
        rx+=temprx;
        rx*=10;
    }

    rx/=10;

    for(int i=0; i<rysize; i++) {
        long tempry = c.geneValue(Ry[i]);
        ry+=tempry;
        ry*=10;
    }

    ry/=10;

    r = rx + ry;

    ret = abs(f+s-r);

    return ret;
}


void natural_selection(){

    int generation = 0;
    // initialize population
    std::vector<Chromosome> population;
    population.resize(POPULATION);

    while(1){

        // find fitness population:
        long best = fitness(population[0]);
        int bestIndex = 0;

        for(int i=0; i<POPULATION; i++){

            // mutation! : 
            population[i].mutate();

            // search the best from fitness function! :
            long candidate = fitness(population[i]);
            if(candidate<best){
                best = candidate;
                bestIndex = i;
            } 
        }

         // best candidate found!
         if(best == 0){
            std::cout<<"Best Candidate: ";
            population[bestIndex].printGene();
            std::cout<<"Generation: "<<generation<<std::endl;
            break;
        }

        // best candidate so far :
        Chromosome candidateBest = population[bestIndex];
        for(int i=0; i<POPULATION; i++) population[i] = candidateBest;

        generation++;
    }
}

int main(){

    // do the Genetic Algorithm:
    natural_selection();

}

I used Python coding which is similar to [Aditya Raut]. The second line "x=' '.join(i)" may not be necessary. The answer is 1324 \boxed{1324} .

 from itertools import permutations
 for a in permutations("0123456789", 8):
        T = a[0]
        O = a[1]
        M = a[2]
        G = a[3]
        I = a[4]
        V = a[5]
        E = a[6]
        S = a[7]
        if T != "0" and G != "0" and E != "0" and I != "0" and V != "0":
            if int(T+O+M)+int(G+I+V+E+S)==int(M+O+S+T)+int(T+I+M+E):
                print T,O,M,G,I,V,E,S
                print int(E+G+G)+int(I+S)+int(V+E+G)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...