Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is arithmetic Expression that print this Sequence?

8,7,8,7,8,7,.......


C++
x=??;
While(true){
x=[??????];            //Arithmetic Expression.
System.Out.Print(x+",");

}
Posted
Updated 24-May-12 8:35am
v2
Comments
Sergey Alexandrovich Kryukov 24-May-12 14:36pm    
First of all, there is more than one. Did you try yourself? What's the problem?
--SA
Sergey Alexandrovich Kryukov 28-May-12 19:53pm    
Now you can clearly see what I mean by "more than one"... :-)
--SA

Try something like

? = x0
8 = x1 = c - x0 → x0 = c - 8
7 = x2 = c - x1 → x1 = c - 7 = 8 → c = ? → x0 = ?
...
... = xn+1 = c - xn

So, what is c and what is x0?
Too much said?

Have fun!
Cheers
Andi
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-12 15:09pm    
It supposed to be just one expression. The rules of such games are: don't use index (you don't have it actually, this is a while loop), don't create additional variables. What's the problem? I gave a final answer, please see.
--SA
Andreas Gieriet 24-May-12 16:25pm    
Read it carefully: I show a way to solve it with a hypothesis (assume the expression being of the form c - x) and mathematical induction to get the needed set of expressions to solve the initial constant and the constant in the hypothesis.

No additional variables...

Mapping that into the a programming language is peanuts.

c = 15, x_0 = 7
x = 7; ... x = 15 - x; ...

You can do further hypothesis, e.g. x_0 = k, x_(n+1) = a(x_n)^2 + b(x_n) + c

...(after some induction and equation solving)...:
k = 7
a = -1
b = 14
c = -41

x = 7; ... x = 14*x - x*x - 41;...

Or re-arranged:

x = 7; ... x = 8-(x-7)*(x-7); ...

So, you see, not new variables, just some hypothesis ans solving of the coefficients.

Cheers
Andi
Andreas Gieriet 24-May-12 16:32pm    
BTW: I wrote my answer while you wrote yours. What's the problem? There is no "final" answer... ;-)
Sergey Alexandrovich Kryukov 24-May-12 17:47pm    
Just a figure of speech... of course, it's not a problem. Yes, I realized that you put your solution at the same time; sorry for a confusing comment.
So, what is your one single expression. It should be just assignment x = f(x)... is it x = 8-(x-7)*(x-7)?
....

OK, got it. My 5.
--SA
Andreas Gieriet 25-May-12 4:33am    
Thanks for your 5!
This playing around with the polynom solution is not realy meant to be used in real life, of course: why using complicated solutions when there are simple ones. I just like to tweak a bit with math as you may have noticed... ;-)

x = 15 - x; and your solution x = (x%2) + 7; are similarily simple - in real life I would take one or the other, but for sure not a more compilcated one.

As of playing with maths: see also my other comments on more generic solutions.

Cheers
Andi
If you asked for a bit operations expression (&, |, ~) instead of an arithmetic expression (+, -, *, /, %), the particular values have a simple solution:

x = ~x & 0x0F;

Reason: the bit pattern of 7 is 00000111 and of 8 is 00001000 --> 7 and 8 are the inverse bits of each other in the lowest 4 bits.
~00000111 & 00001111 = 00001000
~00001000 & 00001111 = 00000111

Cheers
Andi
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-May-12 19:06pm    
Yes, yes, yes. Another 5.
--SA
The solution of this problem requires a minute or so: x = 7; /* … */ x = (x % 2) + 7; that's it.

[EDIT]

One more solution: x = x + 2 * (x & 1) - 1; it can be the fastest on the CPUs where division operators are much slower than additive ones; and bit mask operators are always fast.

[EDIT]

More exactly, even faster solution, thanks to the great note by Andreas Gieriet, would be this: x = x + ((x & 1) << 1) - 1;

—SA
 
Share this answer
 
v5
Comments
Wendelius 24-May-12 15:10pm    
Nice :)
Sergey Alexandrovich Kryukov 24-May-12 17:44pm    
Thank you, Mika.
--SA
Andreas Gieriet 25-May-12 4:43am    
My 5: simple and effective.
Andi
Sergey Alexandrovich Kryukov 25-May-12 11:43am    
Thank you very much, Andreas.
--SA
Sergey Alexandrovich Kryukov 28-May-12 18:00pm    
By the way: one more solution, probably the fastest -- want to take a look?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900