Please could you indent above definition so, that one can grasp it better?C- and Scheme-code below from my old source.If Jacobi-symbol were naturally extended to all natural numbers, not just odd ones, then one might compute many of the "Jacobi-sequences" I computedfor the odd numbers:Here's our C-version. The teaching: Never implement mathematician's or logician's elegant formulae directly, without thinking! Working in GF(2) with XOR & AND is much more natural for computers than working in isomorphic multiplicative group of {+1, -1} with MUL. We also convert the recursion in our formula to a simple loop. q should be always odd! int js_ULI(ULI p,ULI q) { int s = 0; /* 0 in bit-1 stands for +1, 1 in bit-1 for -1. */ ULI new_p; loop: if(0 == p) { return(p); } if(1 == p) { return(1-(s&2)); } /* Convert 1 in bit-1 to -1, 0 to +1. */ if(p&1) /* We have an odd p. */ { /* If both p & q are 3 mod 4, then sign changes, otherwise stays same: */ s ^= (p&q); /* Only the bit-1 is significant, others are ignored. */ new_p = q % p; /* Could we have a simple - here as with Euclid? */ q = p; p = new_p; goto loop; } else /* We have an even p. So (2k/q) = (2/q)*(k/q) */ { /* where (2/q) = 1 if q is +-1 mod 8 and -1 if q is +-3 mod 8. */ /* I.e. sign changes only if q's lower bits are (011) or (101), i.e. if the bit-1 and bit-2 xored yield 1. */ s ^= (q^(q>>1)); /* Thus, this does it. */ p >>= 1; goto loop; } } And Scheme-version: (define (fix:jacobi-symbol p q) (if (not (and (fix:fixnum? p) (fix:fixnum? q) (fix:= 1 (fix:and q 1)))) (error "fix:jacobi-symbol: args must be fixnums, and 2. arg should be odd: " p q ) (let loop ((p p) (q q) (s 0)) ;; 0 in bit-1 stands for +1, 1 in bit-1 for -1. (cond ((fix:zero? p) 0) ((fix:= 1 p) (fix:- 1 (fix:and s 2))) ((fix:= 1 (fix:and p 1)) ;; Odd p ? (loop (fix:remainder q p) p (fix:xor s (fix:and p q))) ) (else ;; It's even, get rid of one 2: (loop (fix:lsh p -1) q (fix:xor s (fix:xor q (fix:lsh q -1)))) ) ) ) ) )
AK> "Please could you indent above definition so, that one cangrasp it better?"This is a problem with the new design of the Google groups.In the old groups you could change the display to fixed widthfonts and everything was all right. This option is gone as faras I see. Now you are forced to format the text. This is notnice. In particular how can the old posts which rely on fixedwidth fonts ever be read properly?AK> "C- and Scheme-code below from my old source."Thanks for the cute implementations!AK> "If Jacobi-symbol were naturally extended to all natural numbers,not just odd ones..."Well, but this is what Kronecker did. People seem to be happywith his extension. Are you not?AK> ".. then one might compute many of the "Jacobi-sequences"I computed for the odd numbers.."Is there a sequence which would profit from this in particular?
AK> "Please could you indent above definition so, that one cangrasp it better?"This is a problem with the new design of the Google groups.In the old groups you could change the display to fixed widthfonts and everything was all right. This option is gone as faras I see. Now you are forced to format the text. This is notnice. In particular how can the old posts which rely on fixedwidth fonts ever be read properly?AK> "C- and Scheme-code below from my old source."Thanks for the cute implementations!AK> "If Jacobi-symbol were naturally extended to all natural numbers,not just odd ones..."Well, but this is what Kronecker did. People seem to be happywith his extension. Are you not?
AK> ".. then one might compute many of the "Jacobi-sequences"I computed for the odd numbers.."Is there a sequence which would profit from this in particular?
maanantai, 6. elokuuta 2012 12.14.45 UTC+3 Peter Luschny kirjoitti:AK> "If Jacobi-symbol were naturally extended to all natural numbers,not just odd ones..."Well, but this is what Kronecker did. People seem to be happywith his extension. Are you not?I don't know, I haven't tested it yet. See below.
I will show in this post three examples comparing the performanceof the three algorithms given above* EJ = ExtendedJacobi* Antti* CohenI have slightly tuned EJ by inserting as a preparation stepthe reduction of n to an odd value, exactly as it is done inCohen's algorithm, i.e. by insertingif is_even(n) :v = 0while is_even(n): v += 1; n = n/2r = 1 if is_even(v) else [0,1,0,-1,0,-1,0,1][a&7]if debug: loops += 1; print(a,n,r,loops)before the while-loop.
(define (gcd a b)(cond ((zero? b) a)((>= a b) (gcd b (- a b)))(else (gcd (- b a) a))))can be also "unrolled" in such a way, that for example, two "originaliteration steps" will be "executed" in one "iteration" (cycle) of theresulting finite state machine (a temporary syntactical kludge needed for this):
(define (gcd_once_unrolled a b)(let-unrolled 1 loop ((a a) (b b))(cond ((zero? b) a)((>= a b) (loop b (- a b)))(else (loop (- b a) a)))))
Compiled to Verilog, this can be found here:
How will the ratio Motzkin/NonMotzkin develop?100 : 50/501000 : 298/70210000 : 1999/8001100000 : ?/?
...
For example, because the elementary number theory says that for allodd primes p, the Legendre symbol L(i/p), i ranging from 1 to p-1 getsequal number of times the value +1 and -1, then it's clear that drawinga graph on a square grid using those values as directions (either up (=+1) or right (-1))gives us a zig-zag graph from origo to point (p-2,p-2).
keskiviikko, 8. elokuuta 2012 19.01.20 UTC+3 Antti Karttunen kirjoitti:...For example, because the elementary number theory says that for allodd primes p, the Legendre symbol L(i/p), i ranging from 1 to p-1 getsequal number of times the value +1 and -1, then it's clear that drawinga graph on a square grid using those values as directions (either up (=+1) or right (-1))gives us a zig-zag graph from origo to point (p-2,p-2).Of course I meant to say (p-1,p-1). Mea culpa.
However, I didn't start with that, but with this slightly kludgous version:(for drawing "Legendre's candelabras". I don't know anymore whether theylook candelabras or maybe some kind of insects?)