Skip to content

Commit ffaeeb4

Browse files
Inital draft for stringify.
1 parent 8abef38 commit ffaeeb4

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/decimals.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// TODO instead allocate enough space M < log_2(d) + d
2+
// for transient + repetend by multiplying x by b**M and do a single division ?
3+
// That may be too much space in most cases. Though necessary when d is prime.
4+
5+
export function _decimals ( { b , eq , muln , divmod } ) {
6+
7+
return function* ( d , n , x ) {
8+
9+
// Computes the length of the repetend of x/d (1 <= x < d) in base b
10+
// with transient part of size n.
11+
12+
while ( n-- ) {
13+
14+
x = muln(x, b) ;
15+
const [q, r] = divmod(x, d) ;
16+
yield q ;
17+
x = r ;
18+
19+
}
20+
21+
first = x ;
22+
23+
x = muln(x, b) ;
24+
const [q, r] = divmod(x, d) ;
25+
yield q ;
26+
x = r ;
27+
28+
while (!eq(first, x)) {
29+
30+
x = muln(x, b) ;
31+
const [q, r] = divmod(x, d) ;
32+
yield q ;
33+
x = r ;
34+
35+
}
36+
37+
} ;
38+
39+
}

src/stringify.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { _decimals } from './_decimals' ;
2+
import { _transient } from './_transient' ;
3+
4+
export function _stringify ( { b , bfactors , jz , gt1 , eq , muln , divmodn , divmod , egcd } ) {
5+
6+
const tr = _transient( { bfactors , jz , gt1 , divmodn } ) ;
7+
const dec = _decimals( { b , eq , muln , divmod } ) ;
8+
9+
return function ( x , d ) {
10+
11+
const [ left , r ] = divmod(x, d) ;
12+
13+
const { u , v } = egcd(d, r) ;
14+
15+
const [ transient_length , has_repetend ] = tr( v ) ;
16+
17+
const right = [ ...dec(v, transient_length, u) ] ;
18+
19+
return [ left , right ] ;
20+
21+
}
22+
23+
}

src/transient.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// credits https://github.com/aureooms-research/repeating-decimal
2+
3+
export function _transient ( { bfactors , jz , gt1 , divmodn } ) {
4+
5+
return function ( d ) {
6+
7+
// Computes the length of the non repeating part in x / d
8+
// ( for any 1 <= x < d with x and d co-prime ) decimals in
9+
// base b whose prime factors are given. Returns tuple ( n , hasrepetend )
10+
// where n is the length of the non repeating part and hasrepetend
11+
// determines if 1 / d repeats or not.
12+
13+
let n = 0 ;
14+
15+
for ( const f of bfactors ) {
16+
17+
let m = 0 ;
18+
19+
while ( true ) {
20+
21+
const [ q , r ] = divmodn(d, f) ;
22+
23+
if (!jz(r)) break ;
24+
25+
++mm ;
26+
d = q ;
27+
28+
}
29+
30+
n = Math.max(n, m) ;
31+
32+
}
33+
34+
return [ n , gt1(d) ] ;
35+
36+
} ;
37+
38+
}

0 commit comments

Comments
 (0)