Data update
This commit is contained in:
parent
ed705008a8
commit
0df55f9f24
2196 changed files with 32999 additions and 3075 deletions
155
Task/Zeckendorf-arithmetic/Dart/zeckendorf-arithmetic.dart
Normal file
155
Task/Zeckendorf-arithmetic/Dart/zeckendorf-arithmetic.dart
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
class Zeckendorf {
|
||||
int dVal = 0;
|
||||
int dLen = 0;
|
||||
|
||||
Zeckendorf(String x) {
|
||||
var q = 1;
|
||||
var i = x.length - 1;
|
||||
dLen = i ~/ 2;
|
||||
while (i >= 0) {
|
||||
dVal += (x[i].codeUnitAt(0) - '0'.codeUnitAt(0)) * q;
|
||||
q *= 2;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
void a(int n) {
|
||||
var i = n;
|
||||
while (true) {
|
||||
if (dLen < i) dLen = i;
|
||||
var j = (dVal >> (i * 2)) & 3;
|
||||
switch (j) {
|
||||
case 0:
|
||||
case 1:
|
||||
return;
|
||||
case 2:
|
||||
if (((dVal >> ((i + 1) * 2)) & 1) != 1) return;
|
||||
dVal += 1 << (i * 2 + 1);
|
||||
return;
|
||||
case 3:
|
||||
dVal &= ~(3 << (i * 2));
|
||||
b((i + 1) * 2);
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void b(int pos) {
|
||||
if (pos == 0) {
|
||||
this.increment();
|
||||
return;
|
||||
}
|
||||
if (((dVal >> pos) & 1) == 0) {
|
||||
dVal += 1 << pos;
|
||||
a(pos ~/ 2);
|
||||
if (pos > 1) a(pos ~/ 2 - 1);
|
||||
} else {
|
||||
dVal &= ~(1 << pos);
|
||||
b(pos + 1);
|
||||
b(pos - (pos > 1 ? 2 : 1));
|
||||
}
|
||||
}
|
||||
|
||||
void c(int pos) {
|
||||
if (((dVal >> pos) & 1) == 1) {
|
||||
dVal &= ~(1 << pos);
|
||||
return;
|
||||
}
|
||||
c(pos + 1);
|
||||
if (pos > 0)
|
||||
b(pos - 1);
|
||||
else
|
||||
this.increment();
|
||||
}
|
||||
|
||||
Zeckendorf increment() {
|
||||
dVal += 1;
|
||||
a(0);
|
||||
return this;
|
||||
}
|
||||
|
||||
void operator + (Zeckendorf other) {
|
||||
for (var gn = 0; gn < (other.dLen + 1) * 2; gn++) {
|
||||
if (((other.dVal >> gn) & 1) == 1) b(gn);
|
||||
}
|
||||
}
|
||||
|
||||
void operator - (Zeckendorf other) {
|
||||
for (var gn = 0; gn < (other.dLen + 1) * 2; gn++) {
|
||||
if (((other.dVal >> gn) & 1) == 1) c(gn);
|
||||
}
|
||||
while (dLen > 0 && (((dVal >> dLen * 2) & 3) == 0)) dLen--;
|
||||
}
|
||||
|
||||
|
||||
void operator * (Zeckendorf other) {
|
||||
var na = other.copy();
|
||||
var nb = other.copy();
|
||||
Zeckendorf nt;
|
||||
var nr = Zeckendorf("0");
|
||||
for (var i = 0; i <= (dLen + 1) * 2; i++) {
|
||||
if (((dVal >> i) & 1) > 0) nr + nb;
|
||||
nt = nb.copy();
|
||||
nb + na;
|
||||
na = nt.copy();
|
||||
}
|
||||
dVal = nr.dVal;
|
||||
dLen = nr.dLen;
|
||||
}
|
||||
|
||||
int compareTo(Zeckendorf other) {
|
||||
return dVal.compareTo(other.dVal);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
if (dVal == 0) return "0";
|
||||
var sb = StringBuffer(dig1[(dVal >> (dLen * 2)) & 3]);
|
||||
for (var i = dLen - 1; i >= 0; i--) {
|
||||
sb.write(dig[(dVal >> (i * 2)) & 3]);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
Zeckendorf copy() {
|
||||
var z = Zeckendorf("0");
|
||||
z.dVal = dVal;
|
||||
z.dLen = dLen;
|
||||
return z;
|
||||
}
|
||||
|
||||
static final List<String> dig = ["00", "01", "10"];
|
||||
static final List<String> dig1 = ["", "1", "10"];
|
||||
}
|
||||
|
||||
void main() {
|
||||
print("Addition:");
|
||||
var g = Zeckendorf("10");
|
||||
g + Zeckendorf("10");
|
||||
print(g);
|
||||
g + Zeckendorf("10");
|
||||
print(g);
|
||||
g + Zeckendorf("1001");
|
||||
print(g);
|
||||
g + Zeckendorf("1000");
|
||||
print(g);
|
||||
g + Zeckendorf("10101");
|
||||
print(g);
|
||||
|
||||
print("\nSubtraction:");
|
||||
g = Zeckendorf("1000");
|
||||
g - Zeckendorf("101");
|
||||
print(g);
|
||||
g = Zeckendorf("10101010");
|
||||
g - Zeckendorf("1010101");
|
||||
print(g);
|
||||
|
||||
print("\nMultiplication:");
|
||||
g = Zeckendorf("1001");
|
||||
g * Zeckendorf("101");
|
||||
print(g);
|
||||
g = Zeckendorf("101010");
|
||||
g + Zeckendorf("101");
|
||||
print(g);
|
||||
}
|
||||
|
|
@ -50,7 +50,7 @@ sealed struct ZeckendorfNumber
|
|||
0 { ^ self }
|
||||
1 { ^ self }
|
||||
2 {
|
||||
ifnot ((dVal $shr ((i + 1) * 2)).allMask:1)
|
||||
ifnot ((dVal $shr ((i + 1) * 2)).allMask(1))
|
||||
{
|
||||
^ self
|
||||
};
|
||||
|
|
@ -81,7 +81,7 @@ sealed struct ZeckendorfNumber
|
|||
{
|
||||
if (pos == 0) { ^ self.inc() };
|
||||
|
||||
ifnot((dVal $shr pos).allMask:1)
|
||||
ifnot((dVal $shr pos).allMask(1))
|
||||
{
|
||||
dVal += (1 $shl pos);
|
||||
self.a(pos / 2);
|
||||
|
|
@ -98,7 +98,7 @@ sealed struct ZeckendorfNumber
|
|||
|
||||
private c(int pos)
|
||||
{
|
||||
if ((dVal $shr pos).allMask:1)
|
||||
if ((dVal $shr pos).allMask(1))
|
||||
{
|
||||
int tmp := 1 $shl pos;
|
||||
tmp := tmp.bxor(-1);
|
||||
|
|
@ -131,9 +131,9 @@ sealed struct ZeckendorfNumber
|
|||
dVal := v;
|
||||
dLen := l;
|
||||
|
||||
for(int GN := 0, GN < (mLen + 1) * 2, GN += 1)
|
||||
for(int GN := 0; GN < (mLen + 1) * 2; GN += 1)
|
||||
{
|
||||
if ((mVal $shr GN).allMask:1)
|
||||
if ((mVal $shr GN).allMask(1))
|
||||
{
|
||||
self.b(GN)
|
||||
}
|
||||
|
|
@ -151,9 +151,9 @@ sealed struct ZeckendorfNumber
|
|||
dVal := v;
|
||||
dLen := l;
|
||||
|
||||
for(int GN := 0, GN < (mLen + 1) * 2, GN += 1)
|
||||
for(int GN := 0; GN < (mLen + 1) * 2; GN += 1)
|
||||
{
|
||||
if ((mVal $shr GN).allMask:1)
|
||||
if ((mVal $shr GN).allMask(1))
|
||||
{
|
||||
self.c(GN)
|
||||
}
|
||||
|
|
@ -177,7 +177,7 @@ sealed struct ZeckendorfNumber
|
|||
ZeckendorfNumber Nr := 0n;
|
||||
ZeckendorfNumber Nt := 0n;
|
||||
|
||||
for(int i := 0, i < (dLen + 1) * 2, i += 1)
|
||||
for(int i := 0; i < (dLen + 1) * 2; i += 1)
|
||||
{
|
||||
if (((dVal $shr i) & 1) > 0)
|
||||
{
|
||||
|
|
|
|||
182
Task/Zeckendorf-arithmetic/Rust/zeckendorf-arithmetic.rust
Normal file
182
Task/Zeckendorf-arithmetic/Rust/zeckendorf-arithmetic.rust
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
struct Zeckendorf {
|
||||
d_val: i32,
|
||||
d_len: i32,
|
||||
}
|
||||
|
||||
impl Zeckendorf {
|
||||
fn new(x: &str) -> Zeckendorf {
|
||||
let mut d_val = 0;
|
||||
let mut q = 1;
|
||||
let mut i = x.len() as i32 - 1;
|
||||
let d_len = i / 2;
|
||||
while i >= 0 {
|
||||
d_val += (x.chars().nth(i as usize).unwrap() as i32 - '0' as i32) * q;
|
||||
q *= 2;
|
||||
i -= 1;
|
||||
}
|
||||
|
||||
Zeckendorf { d_val, d_len }
|
||||
}
|
||||
|
||||
fn a(&mut self, n: i32) {
|
||||
let mut i = n;
|
||||
loop {
|
||||
if self.d_len < i {
|
||||
self.d_len = i;
|
||||
}
|
||||
let j = (self.d_val >> (i * 2)) & 3;
|
||||
match j {
|
||||
0 | 1 => return,
|
||||
2 => {
|
||||
if ((self.d_val >> ((i + 1) * 2)) & 1) != 1 {
|
||||
return;
|
||||
}
|
||||
self.d_val += 1 << (i * 2 + 1);
|
||||
return;
|
||||
}
|
||||
3 => {
|
||||
let temp = 3 << (i * 2);
|
||||
let temp = !temp;
|
||||
self.d_val &= temp;
|
||||
self.b((i + 1) * 2);
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn b(&mut self, pos: i32) {
|
||||
if pos == 0 {
|
||||
self.inc();
|
||||
return;
|
||||
}
|
||||
if ((self.d_val >> pos) & 1) == 0 {
|
||||
self.d_val += 1 << pos;
|
||||
self.a(pos / 2);
|
||||
if pos > 1 {
|
||||
self.a(pos / 2 - 1);
|
||||
}
|
||||
} else {
|
||||
let temp = 1 << pos;
|
||||
let temp = !temp;
|
||||
self.d_val &= temp;
|
||||
self.b(pos + 1);
|
||||
self.b(pos - if pos > 1 { 2 } else { 1 });
|
||||
}
|
||||
}
|
||||
|
||||
fn c(&mut self, pos: i32) {
|
||||
if ((self.d_val >> pos) & 1) == 1 {
|
||||
let temp = 1 << pos;
|
||||
let temp = !temp;
|
||||
self.d_val &= temp;
|
||||
return;
|
||||
}
|
||||
self.c(pos + 1);
|
||||
if pos > 0 {
|
||||
self.b(pos - 1);
|
||||
} else {
|
||||
self.inc();
|
||||
}
|
||||
}
|
||||
|
||||
fn inc(&mut self) -> &mut Self {
|
||||
self.d_val += 1;
|
||||
self.a(0);
|
||||
self
|
||||
}
|
||||
|
||||
fn copy(&self) -> Zeckendorf {
|
||||
Zeckendorf {
|
||||
d_val: self.d_val,
|
||||
d_len: self.d_len,
|
||||
}
|
||||
}
|
||||
|
||||
fn plus_assign(&mut self, other: &Zeckendorf) {
|
||||
for gn in 0..(other.d_len + 1) * 2 {
|
||||
if ((other.d_val >> gn) & 1) == 1 {
|
||||
self.b(gn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn minus_assign(&mut self, other: &Zeckendorf) {
|
||||
for gn in 0..(other.d_len + 1) * 2 {
|
||||
if ((other.d_val >> gn) & 1) == 1 {
|
||||
self.c(gn);
|
||||
}
|
||||
}
|
||||
while (((self.d_val >> self.d_len * 2) & 3) == 0) || (self.d_len == 0) {
|
||||
self.d_len -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn times_assign(&mut self, other: &Zeckendorf) {
|
||||
let mut na = other.copy();
|
||||
let mut nb = other.copy();
|
||||
let mut nt;
|
||||
let mut nr = Zeckendorf::new("0");
|
||||
for i in 0..(self.d_len + 1) * 2 {
|
||||
if ((self.d_val >> i) & 1) > 0 {
|
||||
nr.plus_assign(&nb);
|
||||
}
|
||||
nt = nb.copy();
|
||||
nb.plus_assign(&na);
|
||||
na = nt.copy(); // `na` is now mutable, so this reassignment is allowed
|
||||
}
|
||||
self.d_val = nr.d_val;
|
||||
self.d_len = nr.d_len;
|
||||
}
|
||||
|
||||
fn to_string(&self) -> String {
|
||||
if self.d_val == 0 {
|
||||
return "0".to_string();
|
||||
}
|
||||
|
||||
let dig = ["00", "01", "10"];
|
||||
let dig1 = ["", "1", "10"];
|
||||
|
||||
let idx = (self.d_val >> (self.d_len * 2)) & 3;
|
||||
let mut sb = String::from(dig1[idx as usize]);
|
||||
for i in (0..self.d_len).rev() {
|
||||
let idx = (self.d_val >> (i * 2)) & 3;
|
||||
sb.push_str(dig[idx as usize]);
|
||||
}
|
||||
sb
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Addition:");
|
||||
let mut g = Zeckendorf::new("10");
|
||||
g.plus_assign(&Zeckendorf::new("10"));
|
||||
println!("{}", g.to_string());
|
||||
g.plus_assign(&Zeckendorf::new("10"));
|
||||
println!("{}", g.to_string());
|
||||
g.plus_assign(&Zeckendorf::new("1001"));
|
||||
println!("{}", g.to_string());
|
||||
g.plus_assign(&Zeckendorf::new("1000"));
|
||||
println!("{}", g.to_string());
|
||||
g.plus_assign(&Zeckendorf::new("10101"));
|
||||
println!("{}", g.to_string());
|
||||
println!();
|
||||
|
||||
println!("Subtraction:");
|
||||
g = Zeckendorf::new("1000");
|
||||
g.minus_assign(&Zeckendorf::new("101"));
|
||||
println!("{}", g.to_string());
|
||||
g = Zeckendorf::new("10101010");
|
||||
g.minus_assign(&Zeckendorf::new("1010101"));
|
||||
println!("{}", g.to_string());
|
||||
println!();
|
||||
|
||||
println!("Multiplication:");
|
||||
g = Zeckendorf::new("1001");
|
||||
g.times_assign(&Zeckendorf::new("101"));
|
||||
println!("{}", g.to_string());
|
||||
g = Zeckendorf::new("101010");
|
||||
g.plus_assign(&Zeckendorf::new("101"));
|
||||
println!("{}", g.to_string());
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import "/trait" for Comparable
|
||||
import "./trait" for Comparable
|
||||
|
||||
class Zeckendorf is Comparable {
|
||||
static dig { ["00", "01", "10"] }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue