Created
July 26, 2021 08:17
-
-
Save CraigTheKiwi/c00b1baa82491cdeae331e19936f5d8a to your computer and use it in GitHub Desktop.
Solutions for Cassidoo Newsletter (July 26)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
import math | |
import re | |
# There are a couple of solutions here, one that can be easily stepped through to understand the | |
# process and allows for error checking to be introduced easily, the other a one liner just because | |
# it's what the cool kids seem to be doing. | |
def zerosEndingFactorial(n): | |
total = 1 | |
for i in range(n, 1, -1): | |
total *= i | |
counter = 0 | |
for i in range(len(str(total))-1, 1, -1): | |
if str(total)[i] == "0": | |
counter += 1 | |
else: | |
break | |
return counter | |
def zerosEndingFactorial2(n): | |
return len(re.split('([1-9]+)',str(math.factorial(n)))[-1]) | |
if __name__ == "__main__": | |
print(zerosEndingFactorial(1)) | |
print(zerosEndingFactorial(5)) | |
print(zerosEndingFactorial(100)) | |
print(zerosEndingFactorial2(1)) | |
print(zerosEndingFactorial2(5)) | |
print(zerosEndingFactorial2(100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment