Square root of a Large Number

Source: Internet
Author: User

Put some immature code and ideas here first

The basic method for manual root number opening: 1. Steps for integer square opening:

(1) Separate the number of square users from the right to the left with an asterisk (|;

(2) obtain the first digit of the square root of arithmetic from the first segment on the left;

(3) subtract the square of the first digit from the first segment, and then write down the second segment of the square to be opened as the first remainder;

(4) Multiply the first digit by 20, remove the first remainder, and use the integer part of the quotient as the test operator (if the integer part is greater than or equal to 10, use 9 as the left operator. If the remainder of the first operator is smaller than the product of the first digit multiplied by 20, the operator 0 is required );

(5) Multiply the sum of the first digit plus the sum of the operator, and if the result is larger than the remainder, subtract 1 from the operator and try again, until the product is less than or equal to the remainder, this operator is the second digit of the square root of the arithmetic value. (6) use the same method to continue to calculate other digits of the square root of the arithmetic value.

 

If you don't understand it, follow the code again.

#include <iostream>using namespace std;#include <stdlib.h>#include <string.h>void sqrt(char *str){double i,r,n;int j,l,size,num,x[1005];size=strlen(str);if (size==1&&str[0]=='0'){cout<<0<<endl;return;}if (size%2==1){n=str[0]-48;l=-1;}else{n=(str[0]-48)*10+str[1]-48;l=0;}r=0,num=0;while (true){i=0;while (i*(i+20*r)<=n)++i;--i;n-=i*(i+20*r);r=r*10+i;x[num]=(int)i;++num;l+=2;if (l>=size)break;n=n*100+(double)(str[l]-48)*10+(double)(str[l+1]-48);}for (j=0; j<num; ++j)cout<<x[j];cout<<endl;}int main(){char ch[1005];while (cin>>ch)sqrt(ch);return 0;}

 

 

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.