-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_test.js
56 lines (48 loc) · 1.18 KB
/
binary_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var Bin = require('./binary');
var BB = require('./bitBlocks');
var numBin = require('./goliath_number_theory')
//returns the sum of the first n binary numbers;
function sum(n){
var x = Bin.tenToBits(n);
var i = Bin.tenToBits(1);
var total = Bin.tenToBits(0);
while (Bin.leq(i, x)){
total = Bin.add(total, i);
i = Bin.inc(i);
console.log(total, i);
console.log(Bin.bitsToString(total), Bin.bitsToString(i));
}
return total;
}
//tester function returning the sum of the first n numbers in the usual way;
function sumInt(n){
var total = 0;
var i = 0;
while (i <= n){
total += i;
i ++;
}
return total;
}
//returns an array of the first n powers of two (0 included)
function pow2(n){
var i = 0;
var powers = [];
while (i <= n){
powers.push(Bin.bitsToString(pow(Bin.tenToBits(2), i)));
i++;
}
return powers;
}
//generates a list of n random numbers in the given range();
function randGen(n, a, b){
let randoms = [];
for(let i = 0; i < n; i++){
randoms.push(Bin.bitsToString(Bin.randomRange(a, b)));
}
return randoms;
}
var a = Bin.stringToBits("32");
var b = Bin.stringToBits("64");
var x = randGen(10, a, b);
console.log(x);