Skip to content

Commit

Permalink
Add Project Euler problem #455 Dlang solution
Browse files Browse the repository at this point in the history
  • Loading branch information
menjaraz authored Aug 28, 2024
1 parent 5396363 commit d86fa1c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pe-0455/dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"authors": [
"menja"
],
"copyright": "Copyright © 2024, menja",
"description": "A minimal D application.",
"license": "proprietary",
"name": "pe-0455"
}
40 changes: 40 additions & 0 deletions pe-0455/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Powers with Trailing Digits
// https://projecteuler.net/problem=455

import std.stdio;
import std.datetime.stopwatch;

ulong modularExponentiation(ulong base, ulong exponent, ulong modulus) {
ulong result = 1;

while (exponent > 0) {
if (exponent % 2 != 0) {
result = (result * base) % modulus;
}
base = (base * base) % modulus;
exponent >>= 1;
}

return result;
}

void main() {
auto timer = StopWatch(AutoStart.yes);

ulong totalSum = 0;

for (ulong number = 2; number <= 1000000; number += !(++number % 10)) {
ulong current, next;

for (current = number; ; current = next) {
next = modularExponentiation(number, current, 1000000000);
if (next == current) break;
}

totalSum += current;
}

writefln("\nProject Euler #455\nAnswer: %s", totalSum);
writefln("Elapsed time: %s milliseconds.\n", timer.peek.total!"msecs"());
}

0 comments on commit d86fa1c

Please sign in to comment.