[Archive!] Pure mathematics, physics, chemistry, etc.: brain-training problems not related to trade in any way - page 511

 

MaxZ:

I understand the changes. It's just that I haven't figured it out yet:

   for(int i=5;i>-1;i--)
     {
      XX|=int(1<<A[i]);
     }
   if(XX==0x7E) {return true;}

"|=" is the logical OR? And then it's a dead end...

"|=" is in this case a bitwise "or". This thing lifts bits by XX at positions equal to A[i].

And if, after the loop, all bits one through six are 1, it means that all digits 1 through 6 have met exactly once in the number X.

0x7E is the hexadecimal representation of the binary number 1111110. (the right bit is zero)

 
jartmailru:

Or maybe it's in C++ ?
.
Validate- a function of a single parameter.
Maybe we should do it the old way... allocate memory and cache a bool
for all valid values of the argument?

In MQL you can open an order in case, by some miracle, the hockey numbers split evenly! :))

But seriously, I am definitely not your equal.


MetaDriver:

"|=" is a bitwise "or" in this case. This thing raises the bits by XX in positions equal to A[i].

And if after the loop all bits one through six are 1, it means that all digits from 1 to 6 occur exactly once in the number X.

0x7E is the hexadecimal representation of the binary number 1111110. (right bit is zero)
That's what bothered me the most. Thank you. Got it!
 
jartmailru:

1. How about C++, though?
.
2. Validate- one parameter function.
Maybe we should do it the old-fashioned way... ...allocate memory and cache a bool
for all valid values of the argument?

1. Well, language and environment aren't really the point. The point is the algorithm itself.

2....Uh...What's more?

 
MaxZ:

But 125 ms is clearly not catching up.

You shouldn't do that. Your reading is 47 ms.

Too bad you don't have a solution, it's hard to compare results... :)))

.

But I still don't understand why we have to compare by characters.

if(A6!=B6 && A5!=B5 && A4!=B4 && A3!=B3 && A2!=B2 && A1!=B1)

?

It would seem that the overlaps in a single position are uncritical?

 
MetaDriver:

You shouldn't do that. Your reading is 47 ms.

Too bad you don't have a solution, it's hard to compare results... :)))

.

But I still don't understand why we have to compare by characters.

if(A6!=B6 && A5!=B5 && A4!=B4 && A3!=B3 && A2!=B2 && A1!=B1)

?

It seems to me that coincidences in a single position are uncritical?

Look at that! :DDD


I was struggling for speed.

I believe that the condition:

if (A6 != B6 && A5 != B5 && A4 != B4 && A3 != B3 && A2 != B2 && A1 != B1)

Will execute many times faster than the following lines:

int A = A6*100000+A5*10000+A4*1000+A3*100+A2*10+A1;
int B = B6*100000+B5*10000+B4*1000+B3*100+B2*10+B1;

And this condition is triggered more than once. It's not a bad gain in speed. Although it may not be so significant. We can remove it and check it. But still I am on the side that this condition justifies itself.

 
MaxZ:


I was struggling for speed:

I believe that the condition:

if (A6 != B6 && A5 != B5 && A4 != B4 && A3 != B3 && A2 != B2 && A1 != B1)

will run many times faster than the following lines:

int A = A6*100000+A5*10000+A4*1000+A3*100+A2*10+A1;
int B = B6*100000+B5*10000+B4*1000+B3*100+B2*10+B1;

And this condition is triggered more than once. It turns out to be a nice gain in performance. Although maybe it's not so considerable. You may remove it and check it. But still I'm on the side that this condition justifies itself

But it seems to be incorrect. For example, if A4==B4 the condition will be false, while the numbers may be different (e.g. 654321 and 124365)
 

corrected, made it like this.

                                    for(int B1=1; B1<=6; B1++)
                                      {
                                       if(B1==B2 || B1==B3 || B1==B4 || B1==B5 || B1==B6) continue;
//                                       if(A6!=B6 && A5!=B5 && A4!=B4 && A3!=B3 && A2!=B2 && A1!=B1)
                                         {
                                          int A = A6*100000+A5*10000+A4*1000+A3*100+A2*10+A1;
                                          int B = B6*100000+B5*10000+B4*1000+B3*100+B2*10+B1;
                                          if (A==B) continue;
                                          if(MathMod(A,B)==0)
                                             Print(A6,A5,A4,A3,A2,A1,"/",B6,B5,B4,B3,B2,B1,"=",A/B);
                                         }
                                      }
                                   }

Same 47 ms.

So - no need to bother with this condition.

 
MetaDriver:

corrected, made it like this.

Same 47 ms.

So - don't bother with that condition.

Got it wrong... And I'm talking about speed! :)) My head's all screwed up. It's past my bedtime. My local time is 6:00 in the morning...

This is how you do the code:

                                    for (int B1=1; B1<=6; B1++)
                                    {
                                       if (B1==B2 || B1==B3 || B1==B4 || B1==B5 || B1==B6) continue;
                                       if (A6==B6 && A5==B5 && A4==B4 && A3==B3 && A2==B2 && A1==B1) continue;
                                       
                                       int A = A6*100000+A5*10000+A4*1000+A3*100+A2*10+A1;
                                       int B = B6*100000+B5*10000+B4*1000+B3*100+B2*10+B1;
                                       if (A < B) continue;
                                       if (MathMod(A,B)==0)
                                           Print(A6,A5,A4,A3,A2,A1,"/",B6,B5,B4,B3,B2,B1,"=",A/B);
                                    }

That's too much.

                                       if (A==B) continue;
                                       if (MathMod(A,B)==0)

...because the script would have to divide the less by the more...

 

Shit. Volodya And you have to freeze with that? There you go - fire! How many times!

Yours is a pig. Sincerely...

 
MaxZ:

That's how you do it with the code:


Yeah, that's better. 31 ms.

Svinozavr 22.08.2011 01:58

Shit. Volodya And this is to freeze? There you go - quit it! How much you can!

Yours is Pig. Sincerely...

Artist's caprice.... :)

Mercantile motivations get boring in a week. Sometimes you can get off for the love of art. ;)