a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

commit 8ebd35163abf4f000033a3c1f667edc53fffc195
parent d58bd163e61411a420f36a77d42ff0f24f44dc4a
Author: Pablo Emílio Escobar Gaviria <pablo-escobar@riseup.net>
Date:   Sat,  8 Aug 2020 16:35:32 -0300

Removed irrelevant files from the repository

Diffstat:
Melixir/main.ex | 1+
Dextra/High Level Assembly/conjecture.hla | 129-------------------------------------------------------------------------------
Dextra/x86/PROGRAM.ASM | 199-------------------------------------------------------------------------------
Dextra/x86/PROGRAM.S | 199-------------------------------------------------------------------------------
Mrust/main.rs | 22+++++++++++-----------
5 files changed, 12 insertions(+), 538 deletions(-)
diff --git a/elixir/main.ex b/elixir/main.ex
@@ -75,3 +75,4 @@ defmodule Conjecture do
     sum_digits_tail d, (acc + r)
   end 
 end
+
diff --git a/extra/High Level Assembly/conjecture.hla b/extra/High Level Assembly/conjecture.hla
@@ -1,129 +0,0 @@
-program conjecture;
-
-	#includeonce( "stdlib.hhf" )
-	#includeonce( "w32\win32.hhf")
-	
-	?@nodisplay := true;
-	?@noalignstack := true;
-
-// This program is a simple test for the following conjecture:
-
-// Let S: N -> N be the sum of the digits of a positive integer.
-// For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.
-
-// data segments
-storage
-	max			:uns32;
-	sumAB		:uns32;
-	sumA		:uns32;
-	sumB		:uns32;
-	timeStart	:dword;
-	timeEnd		:dword;
-	
-static
-	err			:boolean := false;
-	
-
-// input eax = number to sum
-// returns sum in ebx
-procedure sum_digits;
-
-	// don't build a stack frame for this procedure
-	@nodisplay; @noframe;
-
-begin sum_digits;
-	
-	mov( 10, ecx );
-	xor( ebx, ebx );
-	while( eax ) do
-		xor( edx, edx );
-		div( ecx );
-		add( edx, ebx );
-	endwhile;
-	ret;
-
-end sum_digits;
-
-
-
-begin conjecture;
-
-	// read an unsigned integer from the user
-	forever
-		try
-			stdout.put ("enter an unsigned integer :>" );
-			stdin.getu32();
-			mov( eax, max );
-			break;
-		anyexception;
-			stdout.newln();
-			continue;
-		endtry;
-	endfor;
-	
-	
-	
-	stdout.put ("Loading ..." nl );
-	w.GetTickCount();
-	mov( eax, timeStart );
-	
-	for( xor(esi, esi); esi <= max; inc( esi ) ) do
-		
-		// outer loop esi = 0 to max iterations
-		
-		for( mov(esi, edi); edi <= max; inc( edi ) ) do
-			
-			// inner loop. edi = esi to max iterations
-			
-			// get S(a+b)
-			mov( esi, eax );
-			add( edi, eax );
-			call sum_digits;
-			mov( ebx, sumAB );
-			
-			// get S(a)
-			mov( esi, eax );
-			call sum_digits;
-			mov( ebx, sumA );
-			
-			// get S(b)
-			mov( edi, eax );
-			call sum_digits;
-			mov( ebx, sumB );
-			
-			// get S(a+b) - S(a) - S(b)
-			mov( sumAB, eax );
-			sub( sumA, eax );
-			sub( sumB, eax );
-			
-			// sign extend into edx
-			cdq();
-			mov( 9, ecx );
-			idiv( ecx );	// signed division of edx:eax
-			test( edx, edx ); // is remainder zero?
-			if( @nz ) then
-				// if remainder is not zero, the conjecture is false
-				stdout.put ("Conjecture is disproved, here is the counter example :", (type uns32 eax), nl );
-				mov( true, err );
-				break;
-			endif;
-		endfor;
-		
-		breakif( err );
-		
-	endfor;
-	
-	// if we get here, we looped through all the values and the
-	// conjecture is proved.
-	if( !err ) then
-		
-		w.GetTickCount();
-		mov( eax, timeEnd );
-		mov( timeEnd, eax );
-		sub( timeStart, eax );
-		stdout.put("Loaded in : ", (type uns32 eax), "ms,  [1 Thread, no preprocessing]", nl);		
-		stdout.put ("The conjecture is proved for all natural numbers smaller or equals to ", max, "!", nl );
-
-	endif;
-
-end conjecture;
diff --git a/extra/x86/PROGRAM.ASM b/extra/x86/PROGRAM.ASM
@@ -1,199 +0,0 @@
-.model tiny
-
-.stack 1000h
-
-.data
-        sums_cache dw 25000 DUP(0)
-
-.code
-        org 100h
-
-start:
-        mov  ax, @data
-        mov  ds, ax
-
-        call read_uint ; Retrieve user input  
-        call cache_sums
-        call counterexpl
-
-cache_sums:
-        push ax                 ; Preserve the value of max
-
-        mov  si, offset sums_cache
-
-        mov  cx, 2
-        mul  cx                 ; ax = max * 2
-cache_sums.loop:
-        call sum_digits
-        mov  [si], bx
-
-        add  si, 2
-        dec  ax
-
-        cmp  ax, 0
-        ja   cache_sums.loop    ; while ax > 0, keep looping
-
-        pop  ax                 ; Restore the value of max to ax 
-        ret
-
-; Iterate `a` from 0 to `max`
-; At the start of this routine ax holds the value of max
-counterexpl:
-        mov  cx, 9              ; Set the devident to 9
-counterexpl.loop:
-        call iter
-
-        dec  ax                 ; a -= 1
-        cmp  ax, 0              ; if a > 0, keep looping
-        ja   counterexpl.loop
-
-        call ok
-        ret
-
-; Iterate `b` from `a` to 0
-; At the start of this routine ax holds the value of a
-iter:
-        mov  bx, ax
-iter.loop:
-        call test_pair
-
-        dec  bx
-        cmp  bx, 0
-        jae  iter.loop
-        ret
-
-; At the start of this routine ax holds the value of a
-; and bx holds the value of b
-test_pair:
-        ; Preserve bp and sp
-        push bp
-        mov  bp, sp
-
-        sub  sp, 6              ; Make room for 3 words [S(a +  b), S(a) and S(b)]
-        push ax                 ; Preserve the value of a
-
-        ; S(a + b) = sums_cache[a + b]
-        mov  si, offset sums_cache
-        add  si, ax
-        add  si, ax
-        add  si, bx
-        add  si, bx
-        mov  ax, [si]
-        mov  [bp - 6], ax
-
-        ; S(a) = sums_cache[a]
-        sub  si, bx
-        sub  si, bx
-        mov  ax, [si]
-        mov  [bp - 4], ax
-
-        ; S(b) = sums_cache[b]
-        mov  si, offset sums_cache
-        add  si, bx
-        add  si, bx
-        mov  ax, [si]
-        mov  [bp - 2], ax
-
-        ; Store S(a + b) - S(a) - S(b) in ax
-        mov  ax, [bp - 6]
-        sub  ax, [bp - 4]
-        sub  ax, [bp - 2]
-
-        mov  dx, 0              ; Clear the register where the rest will be stored
-        div  cx
-        pop  ax                 ; Restore the value of a to ax
-
-        cmp  dx, 0              ; if (S(a + b) - S(a) - S(b)) % 9 != 0, goto fail
-        jne  fail
-
-        ; Restore bp and sp
-        mov  sp, bp
-        pop  bp
-        ret
-
-sum_digits:
-        push ax
-
-        mov  cx, 10 ; Store the devident in cx
-        mov  bx, 0 ; Clear the register where the result will be stored
-sum_digits.loop:
-        mov  dx, 0 ; Clear the rest of the division
-
-        div  cx ; Divide ax by cx
-        add  bx, dx ; Add the rest of the division ax/cx to bx
-
-        ; Loop until ax equals 0
-        cmp  ax, 0
-        ja   sum_digits.loop
-
-        pop  ax
-        ret
-
-; Reads the first element of the command-line tail and
-; tries to convert it to an unsigned word
-read_uint:
-        mov  ax, 0
-        mov  cx, 10
-        push si
-
-        ; Move the start of the command-line tail to `si`
-        ;mov  si, ds
-        mov  si, 5eh
-
-read_uint.loop:
-        ; Read a character from the command-line tail
-        mov  bh, 0
-        mov  bl, [si]
-
-        ; Jump out of the loop at the end of the first arg
-        cmp  bl, ' '
-        jmp  read_uint.end
-        cmp  bl, 0dh
-        jmp  read_uint.end
-
-        ; Check if it's a numeric character
-        cmp  bl, 30h
-        jb   short invalid_input
-        cmp  bl, 39h
-        ja   short invalid_input
-
-        ; Convert the character code into a decimal digit
-        sub  bx, 30h
-
-        ; ax = ax * 10 + bx
-        mul  cx
-        add  ax, bx
-
-        ; Increment the pointer to the argument string and keep looping
-        inc  si
-        jmp  read_uint.loop
-
-read_uint.end:
-        pop  si
-        ret
-
-;; Exit with exit-code 0
-ok:
-        mov  al, 0
-        call quit
-        ret
-
-;; Exit with exit-code 1
-fail:
-        mov  al, 1
-        call quit
-        ret
-
-;; Exit with exit-code 2
-invalid_input:
-        mov  al, 2
-
-        call quit
-        ret
-
-;; Exit with exit-code `al`  
-quit:
-        mov  ah, 4ch
-        int  21h
-        ret
-end start
diff --git a/extra/x86/PROGRAM.S b/extra/x86/PROGRAM.S
@@ -1,199 +0,0 @@
-.model tiny
-
-.stack 1000h
-
-.data
-        sums_cache dw 25000 DUP(0)
-
-.code
-        org 100h
-
-start:
-        mov  ax, @data
-        mov  ds, ax
-
-        call read_uint ; Retrieve user input  
-        call cache_sums
-        call counterexpl
-
-cache_sums:
-        push ax                 ; Preserve the value of max
-
-        mov  si, offset sums_cache
-
-        mov  cx, 2
-        mul  cx                 ; ax = max * 2
-cache_sums.loop:
-        call sum_digits
-        mov  [si], bx
-
-        add  si, 2
-        dec  ax
-
-        cmp  ax, 0
-        ja   cache_sums.loop    ; while ax > 0, keep looping
-
-        pop  ax                 ; Restore the value of max to ax 
-        ret
-
-; Iterate `a` from 0 to `max`
-; At the start of this routine ax holds the value of max
-counterexpl:
-        mov  cx, 9              ; Set the devident to 9
-counterexpl.loop:
-        call iter
-
-        dec  ax                 ; a -= 1
-        cmp  ax, 0              ; if a > 0, keep looping
-        ja   counterexpl.loop
-
-        call ok
-        ret
-
-; Iterate `b` from `a` to 0
-; At the start of this routine ax holds the value of a
-iter:
-        mov  bx, ax
-iter.loop:
-        call test_pair
-
-        dec  bx
-        cmp  bx, 0
-        jae  iter.loop
-        ret
-
-; At the start of this routine ax holds the value of a
-; and bx holds the value of b
-test_pair:
-        ; Preserve bp and sp
-        push bp
-        mov  bp, sp
-
-        sub  sp, 6              ; Make room for 3 words [S(a +  b), S(a) and S(b)]
-        push ax                 ; Preserve the value of a
-
-        ; S(a + b) = sums_cache[a + b]
-        mov  si, offset sums_cache
-        add  si, ax
-        add  si, ax
-        add  si, bx
-        add  si, bx
-        mov  ax, [si]
-        mov  [bp - 6], ax
-
-        ; S(a) = sums_cache[a]
-        sub  si, bx
-        sub  si, bx
-        mov  ax, [si]
-        mov  [bp - 4], ax
-
-        ; S(b) = sums_cache[b]
-        mov  si, offset sums_cache
-        add  si, bx
-        add  si, bx
-        mov  ax, [si]
-        mov  [bp - 2], ax
-
-        ; Store S(a + b) - S(a) - S(b) in ax
-        mov  ax, [bp - 6]
-        sub  ax, [bp - 4]
-        sub  ax, [bp - 2]
-
-        mov  dx, 0              ; Clear the register where the rest will be stored
-        div  cx
-        pop  ax                 ; Restore the value of a to ax
-
-        cmp  dx, 0              ; if (S(a + b) - S(a) - S(b)) % 9 != 0, goto fail
-        jne  fail
-
-        ; Restore bp and sp
-        mov  sp, bp
-        pop  bp
-        ret
-
-sum_digits:
-        push ax
-
-        mov  cx, 10 ; Store the devident in cx
-        mov  bx, 0 ; Clear the register where the result will be stored
-sum_digits.loop:
-        mov  dx, 0 ; Clear the rest of the division
-
-        div  cx ; Divide ax by cx
-        add  bx, dx ; Add the rest of the division ax/cx to bx
-
-        ; Loop until ax equals 0
-        cmp  ax, 0
-        ja   sum_digits.loop
-
-        pop  ax
-        ret
-
-; Reads the first element of the command-line tail and
-; tries to convert it to an unsigned word
-read_uint:
-        mov  ax, 0
-        mov  cx, 10
-        push si
-
-        ; Move the start of the command-line tail to `si`
-        ;mov  si, ds
-        mov  si, 5eh
-
-read_uint.loop:
-        ; Read a character from the command-line tail
-        mov  bh, 0
-        mov  bl, [si]
-
-        ; Jump out of the loop at the end of the first arg
-        cmp  bl, ' '
-        jmp  read_uint.end
-        cmp  bl, 0dh
-        jmp  read_uint.end
-
-        ; Check if it's a numeric character
-        cmp  bl, 30h
-        jb   short invalid_input
-        cmp  bl, 39h
-        ja   short invalid_input
-
-        ; Convert the character code into a decimal digit
-        sub  bx, 30h
-
-        ; ax = ax * 10 + bx
-        mul  cx
-        add  ax, bx
-
-        ; Increment the pointer to the argument string and keep looping
-        inc  si
-        jmp  read_uint.loop
-
-read_uint.end:
-        pop  si
-        ret
-
-;; Exit with exit-code 0
-ok:
-        mov  al, 0
-        call quit
-        ret
-
-;; Exit with exit-code 1
-fail:
-        mov  al, 1
-        call quit
-        ret
-
-;; Exit with exit-code 2
-invalid_input:
-        mov  al, 2
-
-        call quit
-        ret
-
-;; Exit with exit-code `al`  
-quit:
-        mov  ah, 4ch
-        int  21h
-        ret
-
diff --git a/rust/main.rs b/rust/main.rs
@@ -14,12 +14,21 @@ const SUCCESS: i32 = 0;
 const FAIL: i32 = 1;
 const INVALID_INPUT: i32 = 2;
 
+macro_rules! parse_arg {
+    ($argv: expr) => {{
+        match $argv.next().and_then(|s| s.trim().parse().ok()) {
+            Some(n) => n,
+            None    => process::exit(INVALID_INPUT)
+        }
+    }}
+}
+
 fn main() {
     let mut args = env::args();
     args.next();
 
-    let max = parse_arg(args.next());
-    let n_threads = parse_arg(args.next());
+    let max = parse_arg!(args);
+    let n_threads = parse_arg!(args);
 
     if counterexempl(max, n_threads) {
         process::exit(FAIL);
@@ -107,12 +116,3 @@ fn get_sums(max: usize) -> Vec<isize> {
     output
 }
 
-#[inline]
-/// Parse an argument from argv.
-fn parse_arg(arg: Option<String>) -> usize {
-    match arg.and_then(|s| s.trim().parse::<usize>().ok()) {
-        Some(n) => n,
-        None    => process::exit(INVALID_INPUT)
-    }
-}
-