TaUse05LotRatioByEuclid.sleela
lib/advantage/trading-actions-on-verb/05-lot-ratio-by-euclid/TaUse05LotRatioByEuclid.sleela · 134 line(s) · sleela
<< lib/advantage/trading-actions-on-verb/05-lot-ratio-by-euclid/TaUse05LotRatioByEuclid.sleela
SLeeLa trading-action use case.
Definition: Trading long, order ratios: Euclid loop reduces a basket of order sizes to the smallest whole-number ratio, showing every remainder step. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 5 (style, Euclidean). A basket of order sizes 120, 180 and 300 is >
< reduced to its smallest ratio by Euclid loop. Each pass replaces (x, y) by >
< (y, x mod y); the remainder strictly falls, so the loop must end. The >
< reduced sizes times the gcd must give the originals back, and their own gcd >
< must be 1. >
class TaUse05LotRatioByEuclid {
< ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
<< Eleven catalogued attributes with getter/setter pairs. A worker-class
lesson carries the baseline identity record. >>
String metaVersion = "1.0.0";
int metaId = 39305;
String metaAuthor = "Kiro Agent";
String metaDate = "2026-11-11";
String metaCopyright = "SLeeLa project";
String metaTrademark = "none";
String metaCorporation = "none";
< metaOrigin: National | <nation> | International | Galactic >
String metaOrigin = "International";
String metaBirthDate = "2026-11-11";
< metaScienceRating: maturity/robustness on a 1..10 scale >
int metaScienceRating = 5;
< metaNationalSchedule: a real, citable instrument or "none" >
String metaNationalSchedule = "none";
String getMetaVersion() { return metaVersion; }
void setMetaVersion(String v) { metaVersion = v; }
int getMetaId() { return metaId; }
void setMetaId(int v) { metaId = v; }
String getAuthor() { return metaAuthor; }
void setAuthor(String v) { metaAuthor = v; }
String getDate() { return metaDate; }
void setDate(String v) { metaDate = v; }
String getCopyright() { return metaCopyright; }
void setCopyright(String v) { metaCopyright = v; }
String getTrademark() { return metaTrademark; }
void setTrademark(String v) { metaTrademark = v; }
String getCorporation() { return metaCorporation; }
void setCorporation(String v) { metaCorporation = v; }
String getOrigin() { return metaOrigin; }
void setOrigin(String v) { metaOrigin = v; }
String getBirthDate() { return metaBirthDate; }
void setBirthDate(String v) { metaBirthDate = v; }
int getScienceRating() { return metaScienceRating; }
void setScienceRating(int v) { metaScienceRating = v; }
String getNationalSchedule() { return metaNationalSchedule; }
void setNationalSchedule(String v) { metaNationalSchedule = v; }
int passed = 0;
int failed = 0;
void expect(String label, int got, int want) {
if (got == want) {
print(" ok " + label + " = " + got);
passed = passed + 1;
} else {
print(" FAIL " + label + " got " + got + " want " + want);
failed = failed + 1;
}
}
void expectTrue(String label, boolean got) {
if (got) {
print(" ok " + label);
passed = passed + 1;
} else {
print(" FAIL " + label);
failed = failed + 1;
}
}
void summary() {
print(" checks passed=" + passed + " failed=" + failed);
}
void act(SLLongBook b, String text, int code) {
print(" " + text + " : " + b.reason(code));
}
void main() {
SLEuclid e = new SLEuclid();
SLLongBook book = new SLLongBook();
print("== Use case 5: lot ratio by Euclid ==");
int[] sizes = new int[3];
sizes[0] = 120;
sizes[1] = 180;
sizes[2] = 300;
int g1 = e.gcd(120, 180);
print(" gcd(120,180) trail: " + e.trail);
expect("passes for the first gcd", e.steps, 3);
expect("gcd of 120 and 180", g1, 60);
int g2 = e.gcd(g1, 300);
print(" gcd(" + g1 + ",300) trail: " + e.trail);
expect("gcd of the basket", g2, 60);
expect("gcdAll agrees", e.gcdAll(sizes, 3), 60);
int[] ratio = new int[3];
int i = 0;
while (i < 3) {
ratio[i] = sizes[i] / g2;
i = i + 1;
}
expect("ratio first", ratio[0], 2);
expect("ratio second", ratio[1], 3);
expect("ratio third", ratio[2], 5);
expectTrue("ratio times gcd gives the sizes back", ratio[0] * g2 == 120 && ratio[1] * g2 == 180 && ratio[2] * g2 == 300);
expect("gcd of the reduced ratio", e.gcdAll(ratio, 3), 1);
print("-- edge cases --");
expect("gcd(0,7)", e.gcd(0, 7), 7);
expect("gcd(0,0)", e.gcd(0, 0), 0);
expect("gcd(-12,18)", e.gcd(0 - 12, 18), 6);
expect("lcm(4,6)", e.lcm(4, 6), 12);
expect("lcm beyond 32 bits is exact in 64", e.lcm(2000000000, 3), 6000000000);
expect("lcm product above the exact limit is refused", e.lcm(5000000000000000, 3), 0 - 1);
expect("lcm input above the exact limit is refused", e.lcm(3000000000000000000, 7), 0 - 1);
expect("gcd input above the exact limit is refused", e.gcd(9007199254740992, 6), 0 - 1);
expect("gcd at the exact limit", e.gcd(9007199254740991, 3), 1);
print("-- place the basket in the ratio, one unit of 10 shares --");
act(book, "buy 20 @ 5000 on day 1", book.buy(1, ratio[0] * 10, 5000, 0));
act(book, "buy 30 @ 5000 on day 1", book.buy(1, ratio[1] * 10, 5000, 0));
act(book, "buy 50 @ 5000 on day 1", book.buy(1, ratio[2] * 10, 5000, 0));
expect("shares held", book.totalQty(), 100);
expectTrue("invariants hold", book.invariantsHold());
summary();
}
}