GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Math/factorial_trailing_zeroes.py at master · letscode-17/coding-problems

Expand file tree

/

factorial_trailing_zeroes.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
42 lines (32 loc) · 911 Bytes
/

factorial_trailing_zeroes.py

Copy path

File metadata and controls

42 lines (32 loc) · 911 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'''
Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Input: 3
Output: 0
Output explanation: 3! = 6, no trailing zero.
Input: 5
Output: 1
Output explanation: 5! = 120, one trailing zero.
=========================================
Find how many 5s are in range 0-N (more explanation in the solution).
Time Complexity: O(logN)
Space Complexity: O(1)
'''
############
# Solution #
############
def trailing_zeroes(n):
# 0s are produced when 2 and 5 are multiplied
# because 2 * 5 = 10
# so you'll need to count how many 2s and 5s are there
# 2s are always more than 5s
# so count just how many 5s are in that range
res = 0
k = 5
# find all powers of 5
# 25 has 2 5s, 125 has 3 5s, etc
while k <= n:
res += n // k
k *= 5
return res
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.