diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-08-13 03:09:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-13 03:09:47 +0100 |
| commit | ff47f3cffe636d6f86f585ca4e033bbdcbddac1f (patch) | |
| tree | e1a8ac938a326a4ea956d42453b2ee5287e4a571 /challenge-175 | |
| parent | 612a7d850061a9a257672b38c3beb654210ff7ee (diff) | |
| parent | 1b33767df1f2dc3031db3f56b0b412c2f8891e64 (diff) | |
| download | perlweeklychallenge-club-ff47f3cffe636d6f86f585ca4e033bbdcbddac1f.tar.gz perlweeklychallenge-club-ff47f3cffe636d6f86f585ca4e033bbdcbddac1f.tar.bz2 perlweeklychallenge-club-ff47f3cffe636d6f86f585ca4e033bbdcbddac1f.zip | |
Merge pull request #6602 from pokgopun/pwc175
pwc175 solution in dart
Diffstat (limited to 'challenge-175')
| -rw-r--r-- | challenge-175/pokgopun/dart/ch-1.dart | 23 | ||||
| -rw-r--r-- | challenge-175/pokgopun/dart/ch-2.dart | 55 |
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)); +} |
