a-conjecture-of-mine
An exercise on polyglossy: the same problem solved on multiple languages
git clone: git://git.pablopie.xyz/a-conjecture-of-mine
Commit
56f7f764d8d3634464d4090b796be66a5df826ef
Parent
603cba9fb6a4b9074f2df50a591e41049a4c0941
Author
Gark Garcia <37553739+GarkGarcia@users.noreply.github.com >
Date
Sun, 10 Mar 2019 11:39:15 -0300
Cleaned up the JS, TS, ruby and python implementations.
Removed unnecessary print statements and system calls to ensure that the measured performances accurately represent the time dedicated by the implementations to the bulk calculations.
Diffstat
5 files changed, 52 insertions, 108 deletions
diff --git a/script.js b/script.js
@@ -26,29 +26,14 @@ function ask(question) {
}
function getCounterExp(max) {
- const starTime = new Date;
-
const counterexmpls = [];
- let loadBar = 0;
-
- for (let a = 1; a <= max; a++) {
-
- // Print the progress on the screen
- const newLoadBar = Math.round(a * 100 / max);
- if (loadBar != newLoadBar) {
- console.clear();
- loadBar = newLoadBar;
- console.log(`LOADING. . . ${loadBar}%`);
- }
+ for (let a = 1; a <= max; a++)
for (let b = a; b <= max; b++) {
const diff = (a + b).sumDigits() - (a.sumDigits() + b.sumDigits());
if (diff % 9 !== 0) counterexmpls.push([a, b]);
}
- }
- const elepsedTime = Math.round((new Date().getTime() - starTime.getTime()) / 100) / 10;
- console.log(`LOADED. . . in ${elepsedTime}s\n`);
return counterexmpls;
}
@@ -58,7 +43,14 @@ console.log("For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an in
ask("What value would you like to test the conjecture for? ").then(ans => {
if (!isNaN(Number(ans))) {
- const max = Math.round(Number(ans)), counterexmpls = getCounterExp(max);
+ console.log("\nLOADING. . .");
+
+ const max = Math.round(Number(ans))
+ , starTime = new Date
+ , counterexmpls = getCounterExp(max)
+ , elepsed = Math.round((new Date().getTime() - starTime.getTime()) / 100) / 10;
+
+ console.log(`LOADED. . . in ${elepsed}s\n`);
if (counterexmpls.length === 0)
console.log(`The conjecture is proved for all natural numbers smaller or equals to ${max}!`);
@@ -66,5 +58,5 @@ ask("What value would you like to test the conjecture for? ").then(ans => {
console.log("The conjecture is disproved! Here are the counter examples:");
console.log(counterexmpls.map(pair => `(${pair[0]}, ${pair[1]})`).join(", "));
}
- } else console.log(`'${ans}' is not a natural number!`);
+ } else console.log(`\n'${ans}' is not a natural number!`);
});
\ No newline at end of file
diff --git a/script.py b/script.py
@@ -3,12 +3,9 @@
# 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.
-import sys, os, time
+import time
from functools import reduce
-# A function for clearing the prompt
-clear = (lambda : os.system("cls")) if sys.platform.startswith("win") else (lambda : os.system("clear"))
-
def digits(n):
return map(lambda c: int(c), list(str(n)))
@@ -16,41 +13,32 @@ def sum_digits(n):
return reduce((lambda a, b: a + b), digits(n))
def get_counterexmpls(n):
- start = time.time()
-
- load_bar = 0
counterexmpls = []
for a in range(n + 1):
-
- # Print the progress on the screen
- if not round(a * 100 / n) == load_bar:
- load_bar = round(a * 100 / n)
- clear()
- print("LOADING. . . {}%".format(load_bar))
-
for b in range(a, n + 1):
diff = sum_digits(a + b) - (sum_digits(a) + sum_digits(b))
if not diff % 9 == 0:
counterexmpls.append((a, b))
-
- # Mesure the elepsed time
- elepsed = time.time() - start
- print("LOADED. . . in {:.1f}s\n".format(elepsed))
return counterexmpls
-print("\nThe following script is a simple test for the following conjecture:\n")
+print("\nThis script is a simple test for the following conjecture:\n")
print("Let S: N -> N be the sum of the digits of a positive integer.")
print("For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.\n")
user_input = input("What value would you like to test the conjecture for? ")
+print("\nLOADING. . .")
try:
maximum = abs(int(user_input))
- clear()
+
+ start = time.time()
counterexmpls = get_counterexmpls(maximum)
+
+ elepsed = time.time() - start
+ print("LOADED. . . in {:.1f}s\n".format(elepsed))
if len(counterexmpls) == 0:
print("The conjecture is proved for all natural numbers smaller or equals to {}!".format(maximum))
diff --git a/script.rb b/script.rb
@@ -1,41 +1,23 @@
class Integer
- def divides n
- return (n / self) * self == n
+ def divides(n)
+ return n % self == 0
end
- def digits base: 10
+ def digits(base: 10)
quotient, remainder = divmod(base)
return quotient == 0 ? [remainder] : [*quotient.digits(base: base), remainder]
end
def sum_digits
- sum = 0
-
- for n in self.digits
- sum += n
- end
-
- return sum
+ return self.digits.inject(0, &:+)
end
end
-def get_counterexpls max
- start_time = Time.now
-
+def get_counterexpls(max)
counterexpls = []
- progress_percent = 0
-
for a in (0..max)
- new_progress = a * 100 / max
-
- if progress_percent != new_progress
- progress_percent = new_progress
- clear()
- puts "LOADING. . . #{new_progress}%"
- end
-
for b in (a..max)
- difference = (a + b).sum_digits() - a.sum_digits() - b.sum_digits()
+ difference = (a + b).sum_digits - a.sum_digits - b.sum_digits
if !9.divides(difference)
counterexpls.push([a, b])
@@ -43,45 +25,32 @@ def get_counterexpls max
end
end
- elepsed_time = Time.now - start_time
- clear()
- puts "LOADED. . . #{progress_percent}% in #{elepsed_time}s\n\n"
-
return counterexpls
end
-def clear
- if /win32|win64|\.NET|windows|cygwin|mingw32/i.match(RUBY_PLATFORM)
- system('cls')
- else
- system('clear')
- end
-end
-
-puts "The following script is a simple test for the following conjecture:\n\n"
+puts "\nThis script is a simple test for the following conjecture:\n\n"
puts "Let S: N -> N be the sum of the digits of a positive integer."
puts "For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.\n\n"
puts "What value would you like to test the conjecture for?"
user_input = gets
+puts "\nLOADING. . ."
# Check if the input provided by the user in an iteger
-if /^(?:-?[1-9]\d*$)|(?:^0)$/.match(user_input)
- max = user_input.to_i
+if /^\d+$/.match(user_input.chomp)
+ max = user_input.chomp.to_i
+ start = Time.now
counterexpls = get_counterexpls(max)
+ elepsed = Time.now - start
+ puts "LOADED. . . in #{elepsed.round(1)}s\n\n"
+
if counterexpls.length == 0
puts "The conjecture is proved for all natural numbers smaller or equals to #{max}!"
else
puts "The conjecture is disproved! Here are the counter examples:"
-
- counterexpls_str = ""
- for pair in counterexpls
- counterexpls_str = "#{counterexpls_str}, (#{pair[0]}, #{pair[1]})"
- end
-
- puts counterexpls_str
+ puts counterexpls.join(", ")
end
else
- puts "'#{user_input.chomp}' isn't an iteger!"
+ puts "'#{user_input.chomp}' isn't a natural number!"
end
\ No newline at end of file
diff --git a/script.ts b/script.ts
@@ -30,24 +30,12 @@ function ask(question: string): Promise<string> {
}
function getCounterExp(max: number) {
- const starTime = new Date(), counterexmpls: [number, number][] = [];
- let loadBar = 0;
-
- for (let a = 0; a <= max; a++) {
-
- const newLoadBar = Math.round(a * 100 / max);
- if (loadBar != newLoadBar) {
- console.clear();
- loadBar = newLoadBar;
- console.log(`LOADING. . . ${loadBar}%`);
- }
+ const counterexmpls: [number, number][] = [];
+ for (let a = 0; a <= max; a++)
for (let b = a; b <= max; b++)
if (!test(a, b)) counterexmpls.push([a, b]);
- }
- const elepsedTime = Math.round((new Date().getTime() - starTime.getTime()) / 100) / 10;
- console.log(`LOADED. . . in ${elepsedTime}s\n`);
return counterexmpls;
}
@@ -57,7 +45,14 @@ console.log("For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an in
ask("What value would you like to test the conjecture for? ").then(ans => {
if (!isNaN(parseInt(ans))) {
- const max = parseInt(ans), counterexmpls = getCounterExp(max);
+ console.log(`\nLOADING. . .`);
+
+ const max = parseInt(ans)
+ , start = new Date
+ , counterexmpls = getCounterExp(max)
+ , elepsed = Math.round((new Date().getTime() - start.getTime()) / 100) / 10;
+
+ console.log(`LOADED. . . in ${elepsed}s\n`);
if (counterexmpls.length == 0)
console.log(`The conjecture is proved for all natural numbers smaller or equals to ${max}!`);