Python: Computing $e$Prime factorization of a numberPrime factorization for integers with up to 9-digit long...

Can I Retrieve Email Addresses from BCC?

Java - What do constructor type arguments mean when placed *before* the type?

A known event to a history junkie

Is a naturally all "male" species possible?

What is the term when two people sing in harmony, but they aren't singing the same notes?

Is exact Kanji stroke length important?

In Star Trek IV, why did the Bounty go back to a time when whales were already rare?

Organic chemistry Iodoform Reaction

What (else) happened July 1st 1858 in London?

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

Indicating multiple different modes of speech (fantasy language or telepathy)

Why is delta-v is the most useful quantity for planning space travel?

The One-Electron Universe postulate is true - what simple change can I make to change the whole universe?

Why isn't KTEX's runway designation 10/28 instead of 9/27?

Partial sums of primes

I2C signal and power over long range (10meter cable)

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Can a malicious addon access internet history and such in chrome/firefox?

Are Warlocks Arcane or Divine?

How can I raise concerns with a new DM about XP splitting?

For airliners, what prevents wing strikes on landing in bad weather?

Is there enough fresh water in the world to eradicate the drinking water crisis?

Greatest common substring

Latex for-and in equation



Python: Computing $e$


Prime factorization of a numberPrime factorization for integers with up to 9-digit long prime factorsNumber of divisors of a factorialSum of prime factors of binomial coefficients over 9000!${}$Generalized Project Euler 2: A sledgehammer to crack a nutRepeatedly multiplying digits until a single digit is obtainedChecking if the whole number is prime, and if all its digits are tooReformatting Latin vocabulary sets from The BridgePrime pair set finder (Project Euler problem 60)(Codewars): Find the Unknown Digit













-1












$begingroup$


I was trying to solve a problem which stated:




Calculate the first 10 digit prime found in consecutive digits of e.




I was able to solve the problem but I did it by using some 10k digits of e available online. So I tried to write a program which calculates digits of e. The problem is that it simply gives the incorrect answer.



The code and the formula I used are:



$$e = sumlimits_{n=0}^{infty}frac{1}{n!} = frac{1}{1} + frac{1}{1} + frac{1}{1 cdot 2} + frac{1}{1cdot 2 cdot 3} + cdots$$



import math

e=0

x=int(input()) #larger this number, more will be the digits of e

for i in range(x):
e+=(1/(math.factorial(i)))

print(e)


When the user inputs 10, the digits returned are 2.7182815255731922 which is not correct.



Can someone explain why my code does not produce the correct result?










share|improve this question









New contributor




Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    If you want a really good aproximation of e, you could always use the beautiful (1+9^-(4^6*7))^3^2^85 which yields 18457734525360901453873570 digits of e (if you calculate with enough precision), and is a pan-digital formula to boot.
    $endgroup$
    – Oscar Smith
    21 hours ago






  • 1




    $begingroup$
    This is being discussed on meta, twice.
    $endgroup$
    – Peilonrayz
    13 hours ago
















-1












$begingroup$


I was trying to solve a problem which stated:




Calculate the first 10 digit prime found in consecutive digits of e.




I was able to solve the problem but I did it by using some 10k digits of e available online. So I tried to write a program which calculates digits of e. The problem is that it simply gives the incorrect answer.



The code and the formula I used are:



$$e = sumlimits_{n=0}^{infty}frac{1}{n!} = frac{1}{1} + frac{1}{1} + frac{1}{1 cdot 2} + frac{1}{1cdot 2 cdot 3} + cdots$$



import math

e=0

x=int(input()) #larger this number, more will be the digits of e

for i in range(x):
e+=(1/(math.factorial(i)))

print(e)


When the user inputs 10, the digits returned are 2.7182815255731922 which is not correct.



Can someone explain why my code does not produce the correct result?










share|improve this question









New contributor




Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    If you want a really good aproximation of e, you could always use the beautiful (1+9^-(4^6*7))^3^2^85 which yields 18457734525360901453873570 digits of e (if you calculate with enough precision), and is a pan-digital formula to boot.
    $endgroup$
    – Oscar Smith
    21 hours ago






  • 1




    $begingroup$
    This is being discussed on meta, twice.
    $endgroup$
    – Peilonrayz
    13 hours ago














-1












-1








-1





$begingroup$


I was trying to solve a problem which stated:




Calculate the first 10 digit prime found in consecutive digits of e.




I was able to solve the problem but I did it by using some 10k digits of e available online. So I tried to write a program which calculates digits of e. The problem is that it simply gives the incorrect answer.



The code and the formula I used are:



$$e = sumlimits_{n=0}^{infty}frac{1}{n!} = frac{1}{1} + frac{1}{1} + frac{1}{1 cdot 2} + frac{1}{1cdot 2 cdot 3} + cdots$$



import math

e=0

x=int(input()) #larger this number, more will be the digits of e

for i in range(x):
e+=(1/(math.factorial(i)))

print(e)


When the user inputs 10, the digits returned are 2.7182815255731922 which is not correct.



Can someone explain why my code does not produce the correct result?










share|improve this question









New contributor




Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I was trying to solve a problem which stated:




Calculate the first 10 digit prime found in consecutive digits of e.




I was able to solve the problem but I did it by using some 10k digits of e available online. So I tried to write a program which calculates digits of e. The problem is that it simply gives the incorrect answer.



The code and the formula I used are:



$$e = sumlimits_{n=0}^{infty}frac{1}{n!} = frac{1}{1} + frac{1}{1} + frac{1}{1 cdot 2} + frac{1}{1cdot 2 cdot 3} + cdots$$



import math

e=0

x=int(input()) #larger this number, more will be the digits of e

for i in range(x):
e+=(1/(math.factorial(i)))

print(e)


When the user inputs 10, the digits returned are 2.7182815255731922 which is not correct.



Can someone explain why my code does not produce the correct result?







python python-3.x






share|improve this question









New contributor




Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 14 hours ago









mkrieger1

1,3631823




1,3631823






New contributor




Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Aaryan DewanAaryan Dewan

1082




1082




New contributor




Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Aaryan Dewan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    If you want a really good aproximation of e, you could always use the beautiful (1+9^-(4^6*7))^3^2^85 which yields 18457734525360901453873570 digits of e (if you calculate with enough precision), and is a pan-digital formula to boot.
    $endgroup$
    – Oscar Smith
    21 hours ago






  • 1




    $begingroup$
    This is being discussed on meta, twice.
    $endgroup$
    – Peilonrayz
    13 hours ago


















  • $begingroup$
    If you want a really good aproximation of e, you could always use the beautiful (1+9^-(4^6*7))^3^2^85 which yields 18457734525360901453873570 digits of e (if you calculate with enough precision), and is a pan-digital formula to boot.
    $endgroup$
    – Oscar Smith
    21 hours ago






  • 1




    $begingroup$
    This is being discussed on meta, twice.
    $endgroup$
    – Peilonrayz
    13 hours ago
















$begingroup$
If you want a really good aproximation of e, you could always use the beautiful (1+9^-(4^6*7))^3^2^85 which yields 18457734525360901453873570 digits of e (if you calculate with enough precision), and is a pan-digital formula to boot.
$endgroup$
– Oscar Smith
21 hours ago




$begingroup$
If you want a really good aproximation of e, you could always use the beautiful (1+9^-(4^6*7))^3^2^85 which yields 18457734525360901453873570 digits of e (if you calculate with enough precision), and is a pan-digital formula to boot.
$endgroup$
– Oscar Smith
21 hours ago




1




1




$begingroup$
This is being discussed on meta, twice.
$endgroup$
– Peilonrayz
13 hours ago




$begingroup$
This is being discussed on meta, twice.
$endgroup$
– Peilonrayz
13 hours ago










1 Answer
1






active

oldest

votes


















8












$begingroup$

First of all, don't panic. 10 is just not enough (indeed, it only gives you 5 decimal places. Try 20, and obtain



2.71828182846


which is much closer.



Now, Python uses a native floating point, which may only give you that many digits of precision (say, 30). To get more, you need to work with another representations; fractions.Fraction looks like a good candidate.



Finally, calls to math.factorial waste too much computing power. It is better to compute factorials as you go, e.g.



    denom = 1
for i in range(1, x):
e += 1 / denom
denom *= i





share|improve this answer











$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Aaryan Dewan is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216116%2fpython-computing-e%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    8












    $begingroup$

    First of all, don't panic. 10 is just not enough (indeed, it only gives you 5 decimal places. Try 20, and obtain



    2.71828182846


    which is much closer.



    Now, Python uses a native floating point, which may only give you that many digits of precision (say, 30). To get more, you need to work with another representations; fractions.Fraction looks like a good candidate.



    Finally, calls to math.factorial waste too much computing power. It is better to compute factorials as you go, e.g.



        denom = 1
    for i in range(1, x):
    e += 1 / denom
    denom *= i





    share|improve this answer











    $endgroup$


















      8












      $begingroup$

      First of all, don't panic. 10 is just not enough (indeed, it only gives you 5 decimal places. Try 20, and obtain



      2.71828182846


      which is much closer.



      Now, Python uses a native floating point, which may only give you that many digits of precision (say, 30). To get more, you need to work with another representations; fractions.Fraction looks like a good candidate.



      Finally, calls to math.factorial waste too much computing power. It is better to compute factorials as you go, e.g.



          denom = 1
      for i in range(1, x):
      e += 1 / denom
      denom *= i





      share|improve this answer











      $endgroup$
















        8












        8








        8





        $begingroup$

        First of all, don't panic. 10 is just not enough (indeed, it only gives you 5 decimal places. Try 20, and obtain



        2.71828182846


        which is much closer.



        Now, Python uses a native floating point, which may only give you that many digits of precision (say, 30). To get more, you need to work with another representations; fractions.Fraction looks like a good candidate.



        Finally, calls to math.factorial waste too much computing power. It is better to compute factorials as you go, e.g.



            denom = 1
        for i in range(1, x):
        e += 1 / denom
        denom *= i





        share|improve this answer











        $endgroup$



        First of all, don't panic. 10 is just not enough (indeed, it only gives you 5 decimal places. Try 20, and obtain



        2.71828182846


        which is much closer.



        Now, Python uses a native floating point, which may only give you that many digits of precision (say, 30). To get more, you need to work with another representations; fractions.Fraction looks like a good candidate.



        Finally, calls to math.factorial waste too much computing power. It is better to compute factorials as you go, e.g.



            denom = 1
        for i in range(1, x):
        e += 1 / denom
        denom *= i






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        vnpvnp

        40.4k232102




        40.4k232102






















            Aaryan Dewan is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Aaryan Dewan is a new contributor. Be nice, and check out our Code of Conduct.













            Aaryan Dewan is a new contributor. Be nice, and check out our Code of Conduct.












            Aaryan Dewan is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216116%2fpython-computing-e%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Webac Holding Inhaltsverzeichnis Geschichte | Organisationsstruktur | Tochterfirmen |...

            What's the meaning of a knight fighting a snail in medieval book illustrations?What is the meaning of a glove...

            Salamanca Inhaltsverzeichnis Lage und Klima | Bevölkerungsentwicklung | Geschichte | Kultur und...