Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions 097/non-mersenne-faster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python

from six import print_

MOD = 10000000000


def get_power_modulo(b, e):
if e == 0:
return 1

rec_p = get_power_modulo(b, (e >> 1))

ret = rec_p * rec_p

if e & 0x1 != 0:
ret *= b

return ret % MOD


def print_pow(b, e, msg):
result = get_power_modulo(b, e)
print_("%s : %d ** %d %% MOD = %d" % (msg, b, e, result))

print_pow(2, 5, "2 ** 5 is right.")

print_pow(3, 3, "3 ** 3 is right.")

print_pow(2, 10, "2 ** 10 == 1024 is right.")

print_("Answer = ",
((get_power_modulo(2, 7830457) * 28433 + 1) % MOD))

# Based on:
# https://github.com/shlomif/project-euler/tree/master/project-euler/97
#
# Under the Expat licence.
#
# Copyright 2017 Shlomi Fish
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# --------------------------------------------------------------------------