Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: All languages are equally fit

0 views
Skip to first unread message

LEE Sau Dan

unread,
Nov 6, 2009, 10:44:31 PM11/6/09
to
>>>>> "Herman" == Herman Rubin <hru...@odds.stat.purdue.edu> writes:

Herman> If it was code for purely mathematical operations, and I
Herman> knew the purpose, I suspect that I could "read" it and
Herman> interpret it without knowing the language.

OK. I'll let you figure out what the following Java code (in
particular, function 'magic') calculates! AND WHY does that work!?

class A {
public static void main(String args[]) {
for (String arg: args) {
int a;
try {
a = Integer.parseInt(arg);
System.out.println(a + "=>" + magic(a));
} catch (Exception e) {
System.out.println(arg + "=>" + e);
}
}
}


private static double magic(int z) {
if (z < 0)
throw new IllegalArgumentException("z<0");
double u = 0, v = 1;
double w = 1, x = 0;
for (;z>0; z>>>=1) {
if ((z & 1) == 1) {
final double a = w*u;
final double b = (w+x)*(u+v);
final double c = x*v;
w = c+a;
x = b-a;
}

final double a = u*u;
final double b = (u+v)*(u+v);
final double c = v*v;
u = c+a;
v = b-a;
}
return x;
}
}

--
Lee Sau Dan 李守敦 ~{@nJX6X~}

E-mail: dan...@informatik.uni-freiburg.de
Home page: http://www.informatik.uni-freiburg.de/~danlee

LEE Sau Dan

unread,
Nov 14, 2009, 7:53:16 AM11/14/09
to
>>>>> "LSD" == LEE Sau Dan <dan...@informatik.uni-freiburg.de> writes:

>>>>> "Herman" == Herman Rubin <hru...@odds.stat.purdue.edu> writes:

Herman> If it was code for purely mathematical operations, and I
Herman> knew the purpose, I suspect that I could "read" it and
Herman> interpret it without knowing the language.

LSD> OK. I'll let you figure out what the following Java code (in
LSD> particular, function 'magic') calculates! AND WHY does that
LSD> work!?

So, 1 week has passed. Have you read and interpreted my code
successfully?

LEE Sau Dan

unread,
Nov 22, 2009, 10:42:11 PM11/22/09
to

>>>>> "LSD" == LEE Sau Dan <dan...@informatik.uni-freiburg.de> writes:
>>>>> "Herman" == Herman Rubin <hru...@odds.stat.purdue.edu> writes:

Herman> If it was code for purely mathematical operations, and I
Herman> knew the purpose, I suspect that I could "read" it and
Herman> interpret it without knowing the language.

LSD> OK. I'll let you figure out what the following Java code (in
LSD> particular, function 'magic') calculates! AND WHY does that
LSD> work!?

[code snipped]

LSD> So, 1 week has passed. Have you read and interpreted my code
LSD> successfully?

A fortnight has passed now. How's your progress?

LEE Sau Dan

unread,
Nov 23, 2009, 7:54:03 PM11/23/09
to
>>>>> "LSD" == LEE Sau Dan <dan...@informatik.uni-freiburg.de> writes:
>>>>> "Herman" == Herman Rubin <hru...@odds.stat.purdue.edu> writes:

Herman> If it was code for purely mathematical operations, and I
Herman> knew the purpose, I suspect that I could "read" it and
Herman> interpret it without knowing the language.

LSD> OK. I'll let you figure out what the following Java code (in
LSD> particular, function 'magic') calculates! AND WHY does that
LSD> work!?

LEE> [code snipped]

LSD> So, 1 week has passed. Have you read and interpreted my code
LSD> successfully?

LSD> A fortnight has passed now. How's your progress?

And here, I've ported the code to GNU bc, in case you aren't familiar
with Java/C/C++-like languages. GNU bc supports arbitrary-precision
arithmetic. This means it won't suffer from overflow problems (until
memory is exhausted).


define magic(z) {
auto a,b,c,u,v,w,x,s;
if (z < 0) return -1;
s = scale; scale = 0;
v = w = 1;
for (;z>0; z/=2) {
if (z % 2) {
a = w*u;
b = (w+x)*(u+v);


c = x*v;
w = c+a;
x = b-a;
}

a = u*u;
b = (u+v)*(u+v);


c = v*v;
u = c+a;
v = b-a;
}

scale = s;
return x;

LEE Sau Dan

unread,
Nov 23, 2009, 7:58:11 PM11/23/09
to
>>>>> "LSD" == LEE Sau Dan <dan...@informatik.uni-freiburg.de> writes:
>>>>> "Herman" == Herman Rubin <hru...@odds.stat.purdue.edu> writes:

Herman> If it was code for purely mathematical operations, and I
Herman> knew the purpose, I suspect that I could "read" it and
Herman> interpret it without knowing the language.

LSD> OK. I'll let you figure out what the following Java code (in
LSD> particular, function 'magic') calculates! AND WHY does that
LSD> work!?

LEE> [code snipped]

LSD> So, 1 week has passed. Have you read and interpreted my code
LSD> successfully?

LSD> A fortnight has passed now. How's your progress?

And this is a port to R5RS Scheme, in case you aren't familiar with
Java/C/C++-like languages. R5RS Scheme supports arbitrary-precision
arithmetic. So, it won't overflow until memory is exhausted.


(define (magic z)
(if (or (< z 0) (not (integer? z)))
-1 ; error!
(do ((u 0) (v 1)
(w 1) (x 0)
(z z (quotient z 2)))
((<= z 0) x)

(if (odd? z)
(let ((a (* w u))
(b (* (+ w x) (+ u v)))
(c (* x v)))
(set! w (+ c a))
(set! x (- b a))))

(let ((a (* u u))
(b (expt (+ u v) 2))
(c (* v v)))
(set! u (+ c a))
(set! v (- b a))))))

LEE Sau Dan

unread,
Dec 6, 2009, 2:46:22 AM12/6/09
to
>>>>> "LSD" == LEE Sau Dan <dan...@informatik.uni-freiburg.de> writes:
>>>>> "Herman" == Herman Rubin <hru...@odds.stat.purdue.edu> writes:

Herman> If it was code for purely mathematical operations, and I
Herman> knew the purpose, I suspect that I could "read" it and
Herman> interpret it without knowing the language.

LSD> OK. I'll let you figure out what the following Java code (in
LSD> particular, function 'magic') calculates! AND WHY does that
LSD> work!?

LSD> [code snipped]

LSD>> So, 1 week has passed. Have you read and interpreted my code
LSD>> successfully?

LSD>>> A fortnight has passed now. How's your progress?

It's almost a month.

You still haven't figured it out? Or you've given up already?

0 new messages