From 645f6594284fc138b8a54647f5fbf96fe3afdac7 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Tue, 22 Feb 2022 07:56:32 +0800 Subject: The Weekly Challenge 153: Dart solution --- challenge-153/cheok-yin-fung/dart/ch-1.dart | 20 +++++++++++++++++++ challenge-153/cheok-yin-fung/dart/ch-2.dart | 30 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-153/cheok-yin-fung/dart/ch-1.dart create mode 100644 challenge-153/cheok-yin-fung/dart/ch-2.dart diff --git a/challenge-153/cheok-yin-fung/dart/ch-1.dart b/challenge-153/cheok-yin-fung/dart/ch-1.dart new file mode 100644 index 0000000000..192902b5c7 --- /dev/null +++ b/challenge-153/cheok-yin-fung/dart/ch-1.dart @@ -0,0 +1,20 @@ +// The Weekly Challenge 153 +// Task 1: Left Factorials +// Usage: dart ch-1.dart + +void main() { + const arg = 10; + var factorial = [1]; + for (int i = 1; i <= arg; i++) { + factorial.add(factorial[i-1]*i); + } + + var leftfactorial = [0]; + + for (int i = 1; i <= arg; i++) { + leftfactorial.add(leftfactorial[i-1]+factorial[i-1]); + } + for (int i = 1; i <= arg; i++) { + print(leftfactorial[i]); + } +} diff --git a/challenge-153/cheok-yin-fung/dart/ch-2.dart b/challenge-153/cheok-yin-fung/dart/ch-2.dart new file mode 100644 index 0000000000..4a54b1fee5 --- /dev/null +++ b/challenge-153/cheok-yin-fung/dart/ch-2.dart @@ -0,0 +1,30 @@ +// The Weekly Challenge 153 +// Task 2: Factorions +// Usage: dart ch-2.dart [Positive Integer] + +void main(List arguments) { +// define factorial + var factorial = [1]; + for (int i = 1; i <= 9; i++) { + factorial.add(factorial[i-1]*i); + } + +// convert input into a list of positive integers < 10 + String mystr = arguments[0]; + var myint = int.parse(mystr); + mystr = myint.toString(); // avoid leading zeros + var digits = []; + mystr.split("").forEach((item) { + digits.add(int.parse(item)); + }); + +// sum + var sum = 0; + digits.forEach((d) { + sum += factorial[d]; + }); + +// wrap-up + print( myint.toString() + " is a factorion: " + + (sum == myint).toString() ); +} -- cgit