Project Euler 80:square Root Digital expansion square root Number expansion

Source: Internet
Author: User

Square Root Digital expansion

It's well known if the square root of a natural number is not an integer and then it's irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.

The square root of is 1.41421356237309504880 ..., and the digital sum of the first one hundred decimal digits is 475.

For the first one hundred natural numbers, find the all of the digital sums of the first one hundred decimal digits for All the irrational square roots.

Square Root Number expansion

It is well known that if the square root of a natural number is not an integer, then it must be an irrational number. The fractional part of such a square root is infinitely non-cyclic.

The square root of 2 is 1.41421356237309504880 ..., and its 100 digits after the decimal point are 475.

For the first 100 natural numbers, the sum of the 100 digits after the decimal point of all irrational squares is calculated.

Solving

Question: How to find 100 decimal places for irrational numbers? This is so unreasonable.

Reference Blog

Give a good way to the blog above

For the number n we need to go to the square root N, the following very interesting laws

def Suqareroot (n,digits):     = 10** (digits+ 1)    = 5*n     = 5 while      b < limit:        if a >= B:            - =             b        +=10 Else            :*=              int (B/10) * + 5    return int (b/100)

Description under:

1. The title asks for the value of the first 100 digits of the decimal point, including the integer digits

2. The above algorithm only the last b/100 is the approximate solution of the square root n, here is the decimal point, only why not B to understand

Java

 PackageLevel3;ImportJava.math.BigInteger;Importjava.util.ArrayList; Public classpe080{voidrun () {intj = 1; intres = 0;  for(inti=1;i<=100;i++){            if(j*j==i) {J++; Continue; } Res+ = Int_sum (SquareRoot (i,100));    } System.out.println (res); }    PrivateInteger int_sum (BigInteger b) {intres = 0; String Str=b.tostring ();  for(intI=0;i<str.length (); i++) {res+ = Str.charat (i)-' 0 '; }        returnRes; }    PrivateBigInteger SquareRoot (intNintdigits) {        //Define upper BoundsBigInteger limit =NewBigInteger ("ten"). Pow (digits+1); BigInteger Five=NewBigInteger ("5"); BigInteger Ten=NewBigInteger ("10"); BigInteger Hunderd=NewBigInteger ("100"); BigInteger a=NewBigInteger (n+ ""). Multiply (five); BigInteger b=five;  while(B.compareto (limit) < 0){            if(A.compareto (b) >=0) {a=A.subtract (b); b=B.add (Ten); }Else{a=a.multiply (hunderd); b=B.divide (Ten). Multiply (Hunderd). Add (five); }        }        returnb.divide (hunderd); }          Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis (); NewPE080 (). run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}

40886
Running Time=0s34ms

Python

ImportTime ;defSuqareroot (n,digits): Limit= 10** (digits+1) A= 5*N B= 5 whileb <Limit:ifa>=b:a-=b b+=10Else: A*= 100b= Int (B/10) * 100 + 5returnInt (b/100)defint_sum (N): Res= SUM (Map (Lambdax:int (x), Unicode (n))) returnResif __name__=='__main__': T0=time.time () limit= 1000000result=0 J= 1 forIinchRange (1,101):        ifJ*j = =i:j+=1Continueresult+ = Int_sum (Suqareroot (i,100))     Printresult T1=time.time ()Print "running Time=", (T1-T0),"s"#40886#running time= 0.039999961853 s            

Such a program is good without moral integrity

 from Import Decimal,getcontextgetcontext (). Prec=102= set (range (2,100))-Set ([4,9,16,25,36,49,64,81  = 0 for in N:        = Decimal (n). sqrt ()        for  in Str (d). Replace (". ","") [:])print(s)

Project Euler 80:square root Digital expansion square root number expanded

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.