a-conjecture-of-mine

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

Commit
bb062c2cea066a67fdd7e3c3b3e0063ecb0954a3
Parent
3080c5e58522c7c1ee9b85214e504117007f0138
Author
Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date

Created a Kotlin implementation.

Diffstat

10 files changed, 111 insertions, 0 deletions

Status File Name N° Changes Insertions Deletions
Added Kotli/.idea/vcs.xml 7 7 0
Added Kotli/src/main.kt 0 0 0
Added Kotlin/.idea/vcs.xml 7 7 0
Added Kotlin/Kotli.iml 13 13 0
Added Kotlin/out/production/Kotlin/META-INF/Kotlin.kotlin_module 0 0 0
Added Kotlin/out/production/Kotlin/conjecture/MainKt.class 0 0 0
Added Kotlin/out/production/Kotlin/conjecture/Option$None.class 0 0 0
Added Kotlin/out/production/Kotlin/conjecture/Option$Some.class 0 0 0
Added Kotlin/out/production/Kotlin/conjecture/Option.class 0 0 0
Added Kotlin/src/main.kt 84 84 0
diff --git a/Kotli/.idea/vcs.xml b/Kotli/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
+  </component>
+</project>+
\ No newline at end of file
diff --git a/Kotli/src/main.kt b/Kotli/src/main.kt
diff --git a/Kotlin/.idea/vcs.xml b/Kotlin/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
+  </component>
+</project>+
\ No newline at end of file
diff --git a/Kotlin/Kotli.iml b/Kotlin/Kotli.iml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="library" name="KotlinJavaRuntime" level="project" />
+  </component>
+</module>+
\ No newline at end of file
diff --git a/Kotlin/out/production/Kotlin/META-INF/Kotlin.kotlin_module b/Kotlin/out/production/Kotlin/META-INF/Kotlin.kotlin_module
Binary files differ.
diff --git a/Kotlin/out/production/Kotlin/conjecture/MainKt.class b/Kotlin/out/production/Kotlin/conjecture/MainKt.class
Binary files differ.
diff --git a/Kotlin/out/production/Kotlin/conjecture/Option$None.class b/Kotlin/out/production/Kotlin/conjecture/Option$None.class
Binary files differ.
diff --git a/Kotlin/out/production/Kotlin/conjecture/Option$Some.class b/Kotlin/out/production/Kotlin/conjecture/Option$Some.class
Binary files differ.
diff --git a/Kotlin/out/production/Kotlin/conjecture/Option.class b/Kotlin/out/production/Kotlin/conjecture/Option.class
Binary files differ.
diff --git a/Kotlin/src/main.kt b/Kotlin/src/main.kt
@@ -0,0 +1,83 @@
+package conjecture
+
+import kotlin.math.absoluteValue
+import kotlin.system.measureTimeMillis
+
+// The following 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 integer.
+
+fun main(args: Array<String>) {
+    println("\nThe following program is a simple test for the following conjecture:\n")
+    println("Let S: N -> N be the sum of the digits of a positive integer.")
+    println("For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.\n")
+    print("What value would you like to test the conjecture for? ")
+
+    val maxStr = readLine().orEmpty()
+    try {
+        val max = maxStr.toInt()
+
+        println("\nLOADING. . .")
+
+        val (elapsed, counter) = counterexample(max)
+        println("LOADED. . . in ${elapsed}ms\n")
+
+        when (counter) {
+            is Option.None -> println("The conjecture is proved for all natural numbers smaller or equals to $max!")
+            is Option.Some<Pair<Int, Int>> -> {
+                val (a, b) = counter.value
+                println("The conjecture is disproved! Here's a counterexample: ($a}, $b)")
+            }
+        }
+
+    } catch (_: NumberFormatException) {
+        println("\n'$maxStr' is not a natural number!")
+    }
+
+}
+
+internal fun counterexample(max: Int): Pair<Long, Option<Pair<Int, Int>>> {
+    var result: Option<Pair<Int, Int>> = Option.None
+
+    return Pair(
+        measureTimeMillis {
+            val sum = sums(max)
+
+            for (a in 0..max)
+                for (b in a..max) {
+                    val diff = sum[a + b] - sum[a] - sum[b]
+
+                    if (diff % 9 != 0)
+                        result = Option.Some(Pair(a, b))
+                }
+        }, result)
+}
+
+fun sums (max: Int): IntArray {
+    val maxRange = 2 * max + 1
+    val sums = IntArray(maxRange)
+
+    for (i in 0 until maxRange)
+        sums[i] = i.sum
+
+    return sums
+}
+
+val Int.sum: Int
+    get() {
+        var sum = 0
+        var num = this.absoluteValue
+
+        while (num > 0) {
+            sum += num % 10
+            num /= 10
+        }
+
+        return sum
+    }
+
+sealed class Option<out T> {
+    object None : Option<Nothing>()
+    data class Some<out A>(val value: A) : Option<A>()
+}+
\ No newline at end of file