{"id":22513855,"url":"https://github.com/emahtab/factorial-trailing-zeroes","last_synced_at":"2026-01-08T03:05:45.494Z","repository":{"id":79525475,"uuid":"258833370","full_name":"eMahtab/factorial-trailing-zeroes","owner":"eMahtab","description":"Find number of trailing zeroes in factorial of a number","archived":false,"fork":false,"pushed_at":"2020-06-26T15:08:45.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:25:40.946Z","etag":null,"topics":["factorial","leetcode","maths","maths-problem","problem-solving"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-25T17:24:49.000Z","updated_at":"2020-06-26T15:08:48.000Z","dependencies_parsed_at":"2023-05-10T17:15:34.341Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/factorial-trailing-zeroes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffactorial-trailing-zeroes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffactorial-trailing-zeroes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffactorial-trailing-zeroes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffactorial-trailing-zeroes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/factorial-trailing-zeroes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952116,"owners_count":20699428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["factorial","leetcode","maths","maths-problem","problem-solving"],"created_at":"2024-12-07T03:14:44.054Z","updated_at":"2026-01-08T03:05:45.487Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Factorial Trailing Zeroes\n## https://leetcode.com/problems/factorial-trailing-zeroes\n\nGiven an integer n, return the number of trailing zeroes in n!.\n```\nExample 1:\n\nInput: 3\nOutput: 0\nExplanation: 3! = 6, no trailing zero.\n\nExample 2:\n\nInput: 5\nOutput: 1\nExplanation: 5! = 120, one trailing zero.\n```\n**Note: Your solution should be in logarithmic time complexity.**\n\n## Approach :\nLets see some examples :\n\n5! = 1 * 2 * 3 * 4 * 5 = 1 trailing zero\n\n7! = 1 * 2 * 3 * 4 * 5 * 6 * 7 = 1 trailing zero\n\n10! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 = 2 trailing zeroes\n\n15! = 1 * ... * 5 * .... * 10 * ... * 15 = 3 trailing zeroes\n\n20! = 1 * ... * 5 * .... * 10 * ... * 15 * ... * 20 = 4 trailing zeroes\n\n25! = 1 * ... * 5 * .... * 10 * ... * 15 * ... * 20 * .... * 25 = 6 trailing zeroes (25 = 5 * 5)\n\nSo basically we have to count how many times 5 will come, while calculating factorial of a number.\nNumber of trailing zeroes will be equal to number of 5's.\n\n\n# Implementation 1 : Naive (Time Limit Exceeded 😀)\n```java\nimport java.math.BigInteger;\n\nclass Solution {\n    public int trailingZeroes(int n) {\n       // Calculate n!\n    BigInteger nFactorial = BigInteger.ONE;\n    for (int i = 2; i \u003c= n; i++) {\n        nFactorial = nFactorial.multiply(BigInteger.valueOf(i));\n    }\n    \n    // Count how many 0's are on the end.\n    int trailingZeroes = 0;\n    while (nFactorial.mod(BigInteger.TEN).equals(BigInteger.ZERO)) {\n        nFactorial = nFactorial.divide(BigInteger.TEN);\n        trailingZeroes++;\n    }\n    \n    return trailingZeroes;\n   }\n}\n```\n# Implementation 2 : \n```java\nclass Solution {\n    public int trailingZeroes(int n) {\n        int trailingZeroes = 0;  \n        while(n \u003e= 5) {\n            n = n /5; \n            trailingZeroes += n;\n        }\n        return trailingZeroes;\n    }\n}\n```\n\n# References :\n1. https://www.geeksforgeeks.org/biginteger-class-in-java\n2. https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html\n3. https://leetcode.com/articles/factorial-trailing-zeroes\n4. https://www.youtube.com/watch?v=3Hdmv_Ym8PI\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffactorial-trailing-zeroes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Ffactorial-trailing-zeroes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffactorial-trailing-zeroes/lists"}