aboutsummaryrefslogtreecommitdiff
path: root/challenge-175
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-175')
-rw-r--r--challenge-175/pokgopun/dart/ch-1.dart23
-rw-r--r--challenge-175/pokgopun/dart/ch-2.dart55
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-175/pokgopun/dart/ch-1.dart b/challenge-175/pokgopun/dart/ch-1.dart
new file mode 100644
index 0000000000..6317f2d8b4
--- /dev/null
+++ b/challenge-175/pokgopun/dart/ch-1.dart
@@ -0,0 +1,23 @@
+void main() {
+ print(LastWeekdayOfMonth(DateTime.sunday, 2022));
+}
+
+class LastWeekdayOfMonth {
+ final int weekday, year, month;
+ final data = <DateTime>[];
+ LastWeekdayOfMonth(this.weekday, this.year, [this.month = 0]) {
+ final mdc = _monthdayCount(year);
+ for (var i = 0; i < mdc.length; i++) {
+ var t = DateTime.utc(year, i + 1, mdc[i]);
+ var o = weekday - t.weekday;
+ data.add(t.add(Duration(days: o > 0 ? o - 7 : o)));
+ }
+ }
+ List<int> _monthdayCount(int year) =>
+ [31, year % 4 == 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+ @override
+ String toString() => [
+ if (month == 0) ...data,
+ if (month > 0 && month <= 12) data[month - 1]
+ ].map((dt) => dt.toString().split(" ")[0]).join("\n");
+}
diff --git a/challenge-175/pokgopun/dart/ch-2.dart b/challenge-175/pokgopun/dart/ch-2.dart
new file mode 100644
index 0000000000..e4bfd8847f
--- /dev/null
+++ b/challenge-175/pokgopun/dart/ch-2.dart
@@ -0,0 +1,55 @@
+int totient(int n) {
+ // totient(1) = 1, also handle totient(0) at the same time as it can be 0 or 1
+ if (n <= 1) {
+ return 1;
+ }
+ // calcuation using GCD
+ int r = 1; // 1 is relatively prime to any number
+ for (int i = 2; i < n; i++) {
+ if (gcdEuclidean(i, n) == 1) {
+ r++;
+ }
+ }
+ return r;
+}
+
+int gcdEuclidean(int a, int b) {
+ while (a != b) {
+ if (a > b) {
+ a -= b;
+ } else {
+ b -= a;
+ }
+ }
+ return a;
+}
+
+bool isPerfect(int n) {
+ if (n < 2 || n % 2 == 0) {
+ return false;
+ }
+ var sum = 1;
+ var t = totient(n);
+ while (t != 1) {
+ sum += t;
+ if (sum > n) {
+ return false;
+ }
+ t = totient(t);
+ }
+ return sum == n;
+}
+
+void main(List<String> args) {
+ int count = args.isNotEmpty ? int.parse(args[0]) : 20;
+ var output = "";
+ for (int i = 3; i < 50000; i++) {
+ if (isPerfect(i)) {
+ output += ", ${i.toString()}";
+ if (--count == 0) {
+ break;
+ }
+ }
+ }
+ print(output.substring(2));
+}