HP Prime and Casio fx-CG 50: Dedekind Sums
Definition
The Dedekind Sum is defined as follows:
Let P and Q be relatively prime integers, that is GCD(P, Q) = 1.
Then S is the Dedekind sum as:
S = Σ( ((I ÷ Q)) × ((P × I ÷ Q)), for I=1 to Q)
The double parenthesis around the terms I ÷ Q and P × I ÷ Q signify a custom function:
(( X )) =
0, if X is an integer
X – FLOOR(X) – 1/2, if X is not an integer
If X is positive, X – INTG(X) – 1/2
HP Prime: DEDEKIND
EXPORT DEDEKIND(p,q)
BEGIN
// 2024-02-21 EWS
LOCAL s,i,a,b;
// Calculation
IF CAS.gcd(p,q)==1 THEN
s:=0;
FOR i FROM 1 TO q DO
a:=i/q;
IF FP(a)==0 THEN
a:=0;
ELSE
a:=a-FLOOR(a)-0.5;
END;
b:=p*i/q;
IF FP(b)==0 THEN
b:=0;
ELSE
b:=b-FLOOR(b)-0.5;
END;
s:=s+a*b;
END;
RETURN s;
ELSE
RETURN "p and q are not relatively prime.";
END;
END;
Casio fx-CG 50: DEDEKIND
244 bytes
Code:
“DEDEKIND SUM: S(P,Q)”
“P”? → P
“Q”? → Q
If GCD(P,Q)≠1
Then
“P AND Q ARE NOT RELATIVELY PRIME”
Stop
For 1→ I To Q
I÷Q → A
Frac A=0 ⇒ 0 → A
Frac A≠0 ⇒ A – Intg A – 0.5 → A
P × I ÷ Q → B
Frac B=0 ⇒ 0 → B
Frac
B≠0 ⇒ B – Intg B – 0.5 → B
S + A × B → S
Next
S
Note: The are 6 spaces between NOT and RELATIVELY to align the text.
Examples
P |
Q |
Results (fraction) |
Results (decimal) |
2 |
17 |
8/17 |
0.4705882353 |
1 |
21 |
95/63 |
1.50793650794 |
9 |
43 |
27/86 |
0.3139534884 |
8 |
67 |
53/134 |
0.3955223881 |
4 |
75 |
649/450 |
1.442222222 |
14 |
57 |
-140/171 |
-0.8187134503 |
Sources
Shipp, R. Dale. “Table of Dedekind Sums” Journal of Research of the National Bureau of Standards-B. Mathematics and Mathematical Physics Vol. 69B, No 4, October-December 1965 https://nvlpubs.nist.gov/nistpubs/jres/69B/jresv69Bn4p259_A1b.pdf
Retrieved February 21, 2024
Weisstein, Eric W. "Dedekind Sum." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/DedekindSum.html
Retrieved February 18, 2024
Eddie
All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.