{"id":20248662,"url":"https://github.com/cslarsen/eulers-totient-function","last_synced_at":"2026-03-27T04:07:44.960Z","repository":{"id":5143990,"uuid":"6310999","full_name":"cslarsen/eulers-totient-function","owner":"cslarsen","description":"A fast implementation of Euler's totient function phi(n) in C++","archived":false,"fork":false,"pushed_at":"2018-01-04T19:46:23.000Z","size":17,"stargazers_count":8,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T22:37:14.520Z","etag":null,"topics":["c-plus-plus","cryptography","mathematics","phi","prime-numbers"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"willy-vvu/reveal.js","license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cslarsen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-10-20T16:20:53.000Z","updated_at":"2025-03-05T05:58:20.000Z","dependencies_parsed_at":"2022-07-05T16:31:42.904Z","dependency_job_id":null,"html_url":"https://github.com/cslarsen/eulers-totient-function","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cslarsen/eulers-totient-function","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Feulers-totient-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Feulers-totient-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Feulers-totient-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Feulers-totient-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cslarsen","download_url":"https://codeload.github.com/cslarsen/eulers-totient-function/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cslarsen%2Feulers-totient-function/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31018554,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-27T03:51:26.850Z","status":"ssl_error","status_checked_at":"2026-03-27T03:51:09.693Z","response_time":164,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["c-plus-plus","cryptography","mathematics","phi","prime-numbers"],"created_at":"2024-11-14T09:49:02.296Z","updated_at":"2026-03-27T04:07:44.944Z","avatar_url":"https://github.com/cslarsen.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"A fast implementation of Euler's Totient Function in C++\n========================================================\n\nThis package consists of\n\n  * a fast gcd function,\n  * a fast prime number sieve and\n  * a fast implementation of Euler's phi function using the above.\n\nThey are fast because\n\n  * we're using the recursive binary gcd algorithm with bit shifts,\n  * bitsets for primality testing and\n  * Lehmer's conjecture, even-odd reductions and multiplicativity for phi.\n\nThe classes and functions are template-based, so you can plug in any integer\ntype you want to, including multiple precision integers (GMPs; although you\nshould see the note below).\n\nYou can also use the gcd function and prime number sieve separately.\nFinally, the code is in pure C++ and only relies on the standard library.\n\nUpdate\n------\n\nI added an unpolished version of `primes_gmp.cpp` that uses GMP for arbitrarily\nlarge integers. It implements Eulers totient function (phi) in C++ using\nlibgmp, and includes an example to find two 256-bit prime numbers. You can\neasily yank that up to any number you like.\n\nDesign\n------\nThe library is designed to be fast at calculating the phi function _many_\ntimes.  For this, it trades memory for speed.  If you just need to calculate\na one-off phi, you probably don't need this library.\n\nThe phi function\n----------------\nFor more information about Euler's totient function, please see\nhttps://en.wikipedia.org/wiki/Euler%27s_totient_function\n\nNote on big integers\n--------------------\n\nFor fast calculations of phi(n), we ideally want a prime number sieve that\ncontains all the prime numbers below sqrt(n).  For very large n, however,\nthis would take up too much memory.\n\nBecause of this, the implementation will happily chug along with a plain\ntrial division when it reaches the end of the number sieve.  This is slow,\nbut at least it works.\n\nThe most glaring problem, though, is that gmplib doesn't work out of the\nbox, because it lacks operators for bit shifting.  This means you need to\nimplement these operators yourself. **Update: See `primes_gmp.cpp`**\n\nAlso, I haven't tested the speed on very large numbers yet.\n\nLicense\n-------\n\nCopyright (c) 2012 Christian Stigen Larsen\nhttp://csl.sublevel3.org\n\nDistributed under the BSD 3-clause license; see the file LICENSE for a copy\nof the full license text.\n\nUsage examples\n--------------\n\nNote that the first time you invoke `phi()` it will start calculating a all\nprimes below a certain number (one million, by default).  This usually takes\na couple of milliseconds, but for larger limits (e.g., a billion) it can\ntake up to several seconds!\n\nTo invoke phi, you need to feed it an upper limit for the prime sieve.  This\nnumber should ideally be the square root of the maximum number you will put\ninto the phi function.  For example, to calculate phi(100) you can do\n`phi\u003c10\u003e(100)`.\n\nBut don't change the limit, though.  Each time you change the limit, it will\ncreate a separate, static prime sieve.  So just `#define` a limit and use\nthat in your programs.\n\nHere's a simple, complete example:\n\n    #include \u003ciostream\u003e\n    #include \u003cinttypes.h\u003e\n    #include \"phi.h\"\n\n    int main()\n    {\n      std::cout \u003c\u003c phi\u003c100\u003e(12) \u003c\u003c std::endl;\n    }\n\nHere's another:\n\n    #include \u003ciostream\u003e\n    #include \u003cinttypes.h\u003e\n    #include \"phi.h\"\n\n    int main()\n    {\n      for ( uint64_t n=1; n\u003c1000000; ++n ) {\n        uint64_t p = phi\u003c10000000\u003e(n);\n        std::cout \u003c\u003c \"phi(\" \u003c\u003c n \u003c\u003c \") = \" \u003c\u003c p \u003c\u003c std::endl;\n      }\n\n      return 0;\n    }\n\nNote that you can blow up the function since it relies on a certain size of\nthe prime number sieve.  It doesn't use exceptions, only assertions in debug\nmode.  To compile without assertions, use an appropriate compiler definition\nfor your system (`-DNDEBUG` for GCC).\n\nBuilding and testing\n--------------------\n\nTo build the test, just type `make check` and hope for the best:\n\n    $ make\n    c++ -W -Wall -O3    test.cpp   -o test\n    bash -c \"time ./test\"\n    Calculating all primes below 10000000\n    phi(0) = 0\n    phi(56789) = 56160\n    phi(113578) = 56160\n    phi(170367) = 112320\n    phi(227156) = 112320\n    phi(283945) = 224640\n    phi(340734) = 112320\n    phi(397523) = 336960\n    phi(454312) = 224640\n    phi(511101) = 336960\n    phi(567890) = 224640\n    phi(624679) = 561600\n    phi(681468) = 224640\n    phi(738257) = 673920\n    phi(795046) = 336960\n    phi(851835) = 449280\n    phi(908624) = 449280\n    phi(965413) = 898560\n    phi(1000000) = 400000\n    phi(1561167) = 1024056\n    phi(2122334) = 1058616\n    phi(2683501) = 2466240\n    phi(3244668) = 870048\n    phi(3805835) = 2767840\n    phi(4367002) = 2183500\n    phi(4928169) = 3285444\n    phi(5489336) = 2733184\n    phi(6050503) = 6050502\n    phi(6611670) = 1627200\n    phi(7172837) = 6128352\n    phi(7734004) = 3697920\n    phi(8295171) = 5412360\n    phi(8856338) = 4428168\n    phi(9417505) = 7534000\n    phi(9978672) = 3023680\n    phi( 78)= 24   phi( 89)= 88   phi(100)= 40   \n    phi(111)= 72   phi(122)= 60   phi(133)=108   \n    phi(144)= 48   phi(155)=120   phi(166)= 82   \n    phi(177)=116   phi(188)= 92   phi(199)=198   \n    phi(210)= 48   phi(221)=192   phi(232)=112   \n    phi(243)=162   phi(254)=126   phi(265)=208   \n    phi(276)= 88   phi(287)=240   phi(298)=148   \n    phi(309)=204   phi(320)=128   phi(331)=330   \n    phi(342)=108   phi(353)=352   phi(364)=144   \n    phi(375)=200   phi(386)=192   phi(397)=396   \n    phi(408)=128   phi(419)=418   phi(430)=168   \n    phi(441)=252   phi(452)=224   phi(463)=462   \n    phi(474)=156   phi(485)=384   phi(496)=240   \n    phi(507)=312   phi(518)=216   phi(529)=506   \n    phi(540)=144   phi(551)=504   phi(562)=280   \n    phi(573)=380   phi(584)=288   phi(595)=384   \n    phi(606)=200   phi(617)=616   phi(628)=312   \n    phi(639)=420   phi(650)=240   phi(661)=660   \n    phi(672)=192   phi(683)=682   phi(694)=346   \n    phi(705)=368   phi(716)=356   phi(727)=726   \n    phi(738)=240   phi(749)=636   phi(760)=288   \n    phi(771)=512   phi(782)=352   phi(793)=720   \n    phi(804)=264   phi(815)=648   phi(826)=348   \n    phi(837)=540   phi(848)=416   phi(859)=858   \n    phi(870)=224   phi(881)=880   phi(892)=444   \n    phi(903)=504   phi(914)=456   phi(925)=720   \n    phi(936)=288   phi(947)=946   phi(958)=478   \n    phi(969)=576   phi(980)=336   phi(991)=990   \n    1 OK:  phi(12) ==\u003e 4 == 4\n    2 OK:  phi(12) ==\u003e 4 == 4\n    3 OK:  phi(12) ==\u003e 4 == 4\n    4 OK:  phi(1234) ==\u003e 616 == 616\n    5 OK:  phi(12345) ==\u003e 6576 == 6576\n    6 OK:  phi(123456) ==\u003e 41088 == 41088\n    7 OK:  phi(1234567) ==\u003e 1224720 == 1224720\n    8 OK:  phi(12345678) ==\u003e 4027392 == 4027392\n    9 OK:  phi(123456789) ==\u003e 82260072 == 82260072\n    10 OK:  phi(1234567890) ==\u003e 329040288 == 329040288\n    11 OK:  phi(1234) ==\u003e 616 == 616\n    12 OK:  phi(12345) ==\u003e 6576 == 6576\n    13 OK:  phi(123456) ==\u003e 41088 == 41088\n    14 OK:  phi(1234567) ==\u003e 1224720 == 1224720\n    15 OK:  phi(12345678) ==\u003e 4027392 == 4027392\n    16 OK:  phi(123456789) ==\u003e 82260072 == 82260072\n    17 OK:  phi(1234567890) ==\u003e 329040288 == 329040288\n    18 OK:  phi(1234) ==\u003e 616 == 616\n    19 OK:  phi(12345) ==\u003e 6576 == 6576\n    20 OK:  phi(123456) ==\u003e 41088 == 41088\n    21 OK:  phi(1234567) ==\u003e 1224720 == 1224720\n    22 OK:  phi(12345678) ==\u003e 4027392 == 4027392\n    23 OK:  phi(123456789) ==\u003e 82260072 == 82260072\n    24 OK:  phi(1234567890) ==\u003e 329040288 == 329040288\n    25 OK:  phi(1234) ==\u003e 616 == 616\n    26 OK:  phi(12345) ==\u003e 6576 == 6576\n    27 OK:  phi(123456) ==\u003e 41088 == 41088\n    28 OK:  phi(1234567) ==\u003e 1224720 == 1224720\n    29 OK:  phi(12345678) ==\u003e 4027392 == 4027392\n    30 OK:  phi(123456789) ==\u003e 82260072 == 82260072\n    31 OK:  phi(1234567890) ==\u003e 329040288 == 329040288\n    32 OK:  phi(1234) ==\u003e 616 == 616\n    33 OK:  phi(12345) ==\u003e 6576 == 6576\n    34 OK:  phi(123456) ==\u003e 41088 == 41088\n    35 OK:  phi(1234567) ==\u003e 1224720 == 1224720\n    36 OK:  phi(12345678) ==\u003e 4027392 == 4027392\n    37 OK:  phi(123456789) ==\u003e 82260072 == 82260072\n    38 OK:  phi(1234567890) ==\u003e 329040288 == 329040288\n    39 OK:  phi(1234) ==\u003e 616 == 616\n    40 OK:  phi(12345) ==\u003e 6576 == 6576\n    41 OK:  phi(123456) ==\u003e 41088 == 41088\n    42 OK:  phi(1234567) ==\u003e 1224720 == 1224720\n    43 OK:  phi(12345678) ==\u003e 4027392 == 4027392\n    44 OK:  phi(123456789) ==\u003e 82260072 == 82260072\n    45 OK:  phi(1234567890) ==\u003e 329040288 == 329040288\n    46 OK:  phi(1234) ==\u003e 616 == 616\n    47 OK:  phi(12345) ==\u003e 6576 == 6576\n    48 OK:  phi(123456) ==\u003e 41088 == 41088\n    49 OK:  phi(1234567) ==\u003e 1224720 == 1224720\n    50 OK:  phi(12345678) ==\u003e 4027392 == 4027392\n    51 OK:  phi(123456789) ==\u003e 82260072 == 82260072\n    52 OK:  phi(1234567890) ==\u003e 329040288 == 329040288\n    53 OK:  phi(12345678901234567890) ==\u003e 3256788124177920000 == 3256788124177920000\n\n    real\t0m0.243s\n    user\t0m0.170s\n    sys\t  0m0.013s\n\nHow to use it in your programs\n------------------------------\n\nJust `#include \"phi.h\"` and you should be set.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcslarsen%2Feulers-totient-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcslarsen%2Feulers-totient-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcslarsen%2Feulers-totient-function/lists"}