Algorithms that compute do not run forever. They are nevertheless capable of computing up to any desired accuracy .
Choose any and compute .
For example (setting ):
Compare the results with
It appears that the multiplication of the input by gives an accuracy of k decimal digits.[note 2]
To compute the (entire) decimal representation of , one can execute an infinite number of times, increasing by a factor at each pass.
Assume that in the next program () the procedure is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude.
Then will print the entire decimal representation of .[note 3]
// Print sqrt(y), without haltingvoidsqrtForever(unsignedinty){unsignedintresult=isqrt(y);printf("%d.",result);// print result, followed by a decimal pointwhile(true)// repeat forever ...{y=y*100;// theoretical example: overflow is ignoredresult=isqrt(y);printf("%d",result%10);// print last digit of result}}
For example, if one computes using binary search, one obtains the sequence
This computation takes 21 iteration steps, whereas linear search (ascending, starting from ) needs 1414 steps.
Algorithm using Newton's method
One way of calculating and is to use Heron's method, which is a special case of Newton's method, to find a solution for the equation , giving the iterative formula
One can prove[citation needed] that is the largest possible number for which the stopping criterion
ensures in the algorithm above.
In implementations which use number formats that cannot represent all rational numbers exactly (for example, floating point), a stopping constant less than 1 should be used to protect against round-off errors.
Domain of computation
Although is irrational for many , the sequence contains only rational terms when is rational. Thus, with this method it is unnecessary to exit the field of rational numbers in order to calculate , a fact which has some theoretical advantages.
Using only integer division
For computing for very large integers n, one can use the quotient of Euclidean division for both of the division operations. This has the advantage of only using integers for each intermediate value, thus making the use of floating point representations of large numbers unnecessary. It is equivalent to using the iterative formula
By using the fact that
one can show that this will reach within a finite number of iterations.
In the original version, one has for , and for . So in the integer version, one has and until the final solution is reached. For the final solution , one has and , so the stopping criterion is .
However, is not necessarily a fixed point of the above iterative formula. Indeed, it can be shown that is a fixed point if and only if is not a perfect square. If is a perfect square, the sequence ends up in a period-two cycle between and instead of converging.
Example implementation in C
// Square root of integerunsignedintint_sqrt(unsignedints){// Zero yields zero// One yields oneif(s<=1)returns;// Initial estimate (must be too high)unsignedintx0=s/2;// Updateunsignedintx1=(x0+s/x0)/2;while(x1<x0)// Bound check{x0=x1;x1=(x0+s/x0)/2;}returnx0;}
Numerical example
For example, if one computes the integer square root of 2000000 using the algorithm above, one obtains the sequence
In total 13 iteration steps are needed. Although Heron's method converges quadratically close to the solution, less than one bit precision per iteration is gained at the beginning. This means that the choice of the initial estimate is critical for the performance of the algorithm.
When a fast computation for the integer part of the binary logarithm or for the bit-length is available (like e.g. std::bit_width in C++20), one should better start at
which is the least power of two bigger than . In the example of the integer square root of 2000000, , , and the resulting sequence is
In this case only four iteration steps are needed.
Digit-by-digit algorithm
The traditional pen-and-paper algorithm for computing the square root is based on working from higher digit places to lower, and as each new digit pick the largest that will still yield a square . If stopping after the one's place, the result computed will be the integer square root.
Using bitwise operations
If working in base 2, the choice of digit is simplified to that between 0 (the "small candidate") and 1 (the "large candidate"), and digit manipulations can be expressed in terms of binary shift operations. With * being multiplication, << being left shift, and >> being logical right shift, a recursive algorithm to find the integer square root of any natural number is:
definteger_sqrt(n:int)->int:assertn>=0,"sqrt works for only non-negative inputs"ifn<2:returnn# Recursive call:small_cand=integer_sqrt(n>>2)<<1large_cand=small_cand+1iflarge_cand*large_cand>n:returnsmall_candelse:returnlarge_cand# equivalently:definteger_sqrt_iter(n:int)->int:assertn>=0,"sqrt works for only non-negative inputs"ifn<2:returnn# Find the shift amount. See also [[find first set]],# shift = ceil(log2(n) * 0.5) * 2 = ceil(ffs(n) * 0.5) * 2shift=2while(n>>shift)!=0:shift+=2# Unroll the bit-setting loop.result=0whileshift>=0:result=result<<1large_cand=(result+1)# Same as result ^ 1 (xor), because the last bit is always 0.iflarge_cand*large_cand<=n>>shift:result=large_candshift-=2returnresult
Traditional pen-and-paper presentations of the digit-by-digit algorithm include various optimizations not present in the code above, in particular the trick of pre-subtracting the square of the previous digits which makes a general multiplication step unnecessary. See Methods of computing square roots § Binary numeral system (base 2) for an example.[1]
Karatsuba square root algorithm
The Karatsuba square root algorithm is a combination of two functions: a public function, which returns the integer square root of the input, and a recursive private function, which does the majority of the work.
The public function normalizes the actual input, passes the normalized input to the private function, denormalizes the result of the private function, and returns that.
The private function takes a normalized input, divides the input bits in half, passes the most-significant half of the input recursively to the private function, and performs some integer operations on the output of that recursive call and the least-significant half of the input to get the normalized output, which it returns.
An example algorithm for 64-bit unsigned integers is below. The algorithm:
Normalizes the input inside u64_isqrt.
Calls u64_normalized_isqrt_rem, which requires a normalized input.
Calls u32_normalized_isqrt_rem with the most-significant half of the normalized input's bits, which will already be normalized as the most-significant bits remain the same.
Continues on recursively until there's an algorithm that's faster when the number of bits is small enough.
u64_normalized_isqrt_rem then takes the returned integer square root and remainder to produce the correct results for the given normalized u64.
u64_isqrt then denormalizes the result.
/// Performs a Karatsuba square root on a `u64`.pubfnu64_isqrt(mutn: u64)-> u64{ifn<=u32::MAXasu64{// If `n` fits in a `u32`, let the `u32` function handle it.returnu32_isqrt(nasu32)asu64;}else{// The normalization shift satisfies the Karatsuba square root// algorithm precondition "a₃ ≥ b/4" where a₃ is the most// significant quarter of `n`'s bits and b is the number of// values that can be represented by that quarter of the bits.//// b/4 would then be all 0s except the second most significant// bit (010...0) in binary. Since a₃ must be at least b/4, a₃'s// most significant bit or its neighbor must be a 1. Since a₃'s// most significant bits are `n`'s most significant bits, the// same applies to `n`.//// The reason to shift by an even number of bits is because an// even number of bits produces the square root shifted to the// left by half of the normalization shift://// sqrt(n << (2 * p))// sqrt(2.pow(2 * p) * n)// sqrt(2.pow(2 * p)) * sqrt(n)// 2.pow(p) * sqrt(n)// sqrt(n) << p//// Shifting by an odd number of bits leaves an ugly sqrt(2)// multiplied in.constEVEN_MAKING_BITMASK: u32=!1;letnormalization_shift=n.leading_zeros()&EVEN_MAKING_BITMASK;n<<=normalization_shift;let(s,_)=u64_normalized_isqrt_rem(n);letdenormalization_shift=normalization_shift/2;returns>>denormalization_shift;}}/// Performs a Karatsuba square root on a normalized `u64`, returning the square/// root and remainder.fnu64_normalized_isqrt_rem(n: u64)-> (u64,u64){constHALF_BITS: u32=u64::BITS>>1;constQUARTER_BITS: u32=u64::BITS>>2;constLOWER_HALF_1_BITS: u64=(1<<HALF_BITS)-1;debug_assert!(n.leading_zeros()<=1,"Input is not normalized: {n} has {} leading zero bits, instead of 0 or 1.",n.leading_zeros());lethi=(n>>HALF_BITS)asu32;letlo=n&LOWER_HALF_1_BITS;let(s_prime,r_prime)=u32_normalized_isqrt_rem(hi);letnumerator=((r_primeasu64)<<QUARTER_BITS)|(lo>>QUARTER_BITS);letdenominator=(s_primeasu64)<<1;letq=numerator/denominator;letu=numerator%denominator;letmuts=(s_prime<<QUARTER_BITS)asu64+q;letmutr=(u<<QUARTER_BITS)|(lo&((1<<QUARTER_BITS)-1));letq_squared=q*q;ifr<q_squared{r+=2*s-1;s-=1;}r-=q_squared;return(s,r);}
In programming languages
Some programming languages dedicate an explicit operation to the integer square root calculation in addition to the general case or can be extended by libraries to this end.
The Bank of New York Mellon CorporationJenisPublikKode emitenNYSE: BKKomponen S&P 100Komponen S&P 500IndustriPerbankanJasa keuanganPendahuluBank of New York (didirikan pada tanggal 9 Juni 1784; 239 tahun lalu (1784-06-09))Mellon FinancialDidirikan1 Juli 2007; 16 tahun lalu (2007-07-01)PendiriAlexander HamiltonAaron BurrThomas MellonKantorpusatJl. Greenwich no. 240Manhattan, New York, Amerika SerikatWilayah operasiSeluruh duniaTokohkunciThomas P. (Todd) Gibbons(CEO)Emily Port...
Argas Klasifikasi ilmiah Kerajaan: Animalia Filum: Arthropoda Kelas: Arachnida Subkelas: Acari Ordo: Ixodida Famili: Argasidae Genus: ArgasLatreille, 1795 Species lihat teks Caplak unggas adalah sejenis serangga caplak yang biasa menyerang unggas dan bergenus Argas. Pranala http://books.google.co.id/books?id=9QnxNifPCQgC&pg=PA104&lpg=PA104&dq=Caplak+Ayam&source=bl&ots=zlyg75n6L9&sig=LpzHuFgEuq1mmLKDSDNJzT1psuA&hl=en&sa=X&ei=cKOmU5G1AtWfugTV4YHACA&ved=0...
Universitas Jenderal Achmad Yani YogyakartaLambang Universitas Jenderal Ahmad Yani YogyakartaMotoAditya Mahatma DaksaJenisPerguruan Tinggi SwastaDidirikan26 Maret 2018RektorProf. Dr. rer.nat.apt. Triana Hertiani, S.Si., M.Si.LokasiSleman, Yogyakarta, IndonesiaWarna Situs webwww.unjaya.ac.idUniversitas Jenderal Achmad Yani Yogyakarta (disingkat dengan Unjaya) merupakan Universitas berlokasi di Yogyakarta di bawah naungan Yayasan Kartika Eka Paksi (YKEP) TNI Angkatan Darat hasil penggabung...
Un certificato di assegnazione rilasciato secondo l'Homestead Act in Nebraska, 1868. L'Homestead Act è stato un provvedimento legislativo degli Stati Uniti secondo il quale venivano assegnati, a chi ne faceva richiesta, 160 acri (65 ettari) di terra demaniale nelle terre selvagge al di fuori dei confini delle tredici colonie originali.[1] La nuova legge prevedeva tre fasi: una domanda di assegnazione, l'impegno a lavorare la terra assegnata e l'ottenimento del titolo di proprietà ...
Map all coordinates using OpenStreetMap Download coordinates as: KML GPX (all coordinates) GPX (primary coordinates) GPX (secondary coordinates) Classroom display of the stages in processing cotton. Preston in Lancashire, England has been associated with cotton since John Horrocks built his first spinning mill, the Yellow factory, in 1791. This was powered by a Bateman & Sherratt engine. Preston mills tended to have their own reservoirs. They spun cotton using hand mules and self-actors ...
Pour les articles homonymes, voir Fernand Grenier (homonymie) et Grenier (homonymie). Fernand Grenier Fonctions Député français 3 avril 1967 – 30 mai 1968(1 an, 1 mois et 27 jours) Élection 12 mars 1967 Circonscription 2e de la Seine-Saint-Denis Législature IIIe (Cinquième République) Groupe politique COM Prédécesseur Circonscription créée Successeur Marcelin Berthelot 9 décembre 1958 – 2 avril 1967(8 ans, 3 mois et 24 jours) Élection 30 novembr...
Державний комітет телебачення і радіомовлення України (Держкомтелерадіо) Приміщення комітетуЗагальна інформаціяКраїна УкраїнаДата створення 2003Керівне відомство Кабінет Міністрів УкраїниРічний бюджет 1 964 898 500 ₴[1]Голова Олег НаливайкоПідвідомчі ор...
Cet article est une ébauche concernant un journaliste français. Vous pouvez partager vos connaissances en l’améliorant (comment ?) selon les recommandations des projets correspondants. Bertrand TessierBiographieNaissance 13 juillet 1960 (63 ans)NantesNationalité françaiseActivités Journaliste, réalisateurAutres informationsA travaillé pour Paris MatchFrance-Soirmodifier - modifier le code - modifier Wikidata Bertrand Tessier, né à Nantes, est un journaliste français, a...
American brand of toothpaste Rembrandt toothpasteRembrandt Intense Stain toothpasteProduct typeToothpasteOwnerRanir LLCCountryUnited StatesIntroduced1990; 34 years ago (1990)DiscontinuedGentle White line, in 2012; 12 years ago (2012)Previous ownersDen-Mat Corp.; Gillette; Johnson & JohnsonWebsiteREMBRANDT Rembrandt toothpaste is an American brand of toothpaste.[1][2] History In 1990, the Rembrandt toothpaste brand was developed and owned...
علي بن محمد بن العباس التوحيدي البغدادي معلومات شخصية اسم الولادة علي الميلاد 310 هـ - 922 مبغداد، العراق الوفاة 414 هـ - 1023 مشيراز الحياة العملية الاسم الأدبي أبو حيان التوحيدي الفترة العصر العباسي الثاني النوع أدبفلسفةتصوف المواضيع فلسفة إسلامية تعلم لدى أبو الحسن الرمان...
العلاقات الكاميرونية الموريتانية الكاميرون موريتانيا الكاميرون موريتانيا تعديل مصدري - تعديل العلاقات الكاميرونية الموريتانية هي العلاقات الثنائية التي تجمع بين الكاميرون وموريتانيا.[1][2][3][4][5] مقارنة بين البلدين هذه مقارنة عامة وم...
American actor (1922–1994) Telly SavalasSavalas in 1973BornAristotelis Savalas(1922-01-21)January 21, 1922Garden City, New York, U.S.DiedJanuary 22, 1994(1994-01-22) (aged 72)Universal City, California, U.S.Resting placeForest Lawn Memorial Park, California, U.S.Occupation(s)Actor, singerYears active1950–1994Spouses Katherine Nicolaides (m. 1948; div. 1957) Marilyn Gardner (m. 1960; div. ...
إيمانويل أومودياغبي معلومات شخصية الميلاد 19 أكتوبر 1985 (39 سنة) نيجيريا الطول 1.85 م (6 قدم 1 بوصة) مركز اللعب وسط الجنسية نيجيريا معلومات النادي النادي الحالي واري وولفز الرقم 16 المسيرة الاحترافية1 سنوات فريق م. (هـ.) 2005–2006 بيندل إنشورانس 2006–2010 هارتلاند 2010– وا...
Dewan Perwakilan Rakyat Daerah Kota DumaiDewan Perwakilan RakyatKota Dumai2019-2024JenisJenisUnikameral SejarahSesi baru dimulai3 September 2019PimpinanKetuaSuprianto, S.H. (Demokrat) sejak 20 Juni 2022 Wakil Ketua IMawardi (PKS) sejak 21 Oktober 2019 Wakil Ketua IIBahari (PDI-P) sejak 21 Oktober 2019 KomposisiAnggota30Partai & kursi PDI-P (4) NasDem (4) Hanura (1) Demokrat (5) PAN (3) Golkar (3) ...
تاريخ بلجيكامعلومات عامةالمنطقة بلجيكا وصفها المصدر كتاب العائلة الشمالي الموسوعة السوفيتية الأرمينية موسوعة سيتين العسكرية التأثيراتأحد جوانب بلجيكا فرع من history of the Low Countries (en) تعديل - تعديل مصدري - تعديل ويكي بيانات تواريخ مهمة في بلجيكا منتصف القرن الأول قبل الميلاد اس...
Voce principale: Prima Divisione 1950-1951. La Prima Divisione fu il massimo campionato regionale di calcio disputato in Piemonte nella stagione 1950-1951. Indice 1 Girone A 1.1 Squadre partecipanti 1.2 Classifica finale 2 Girone B 2.1 Squadre partecipanti 2.2 Classifica finale 3 Girone C 3.1 Squadre partecipanti 3.2 Classifica finale 4 Girone D 4.1 Squadre partecipanti 4.2 Classifica finale 5 Finali regionali 5.1 Verdetti finali 6 Bibliografia 6.1 Giornali 6.2 Libri 7 Voci correlate Girone ...
Cuban-American politician (born 1956) This biography of a living person needs additional citations for verification. Please help by adding reliable sources. Contentious material about living persons that is unsourced or poorly sourced must be removed immediately from the article and its talk page, especially if potentially libelous.Find sources: Emilio T. Gonzalez – news · newspapers · books · scholar · JSTOR (April 2011) (Learn how and when to remove ...
Zeba Bakhtiarزيبا بختيارBakhtiar di PFDC Sunsilk Fashion Week pada 2010LahirZeba BakhtiarSuami/istriAdnan Sami (m. 1993; c. 1997)AnakAzaan Nigar Awards (en) Zeba Bakhtiar (bahasa Urdu: زيبا بختيار) adalah seorang aktris film dan televisi asal Pakistan dan seorang sutradara televisi. Ia memulai debut televisi dengan sebuah drama Korporasi Televisi Pakistan (PTV), Anarkali (1988).[1] Ia memulai debut Bollywood ...
تضم هذه المقالة مصادرَ مُستشهداً بها بشكلٍ عام أو بشكل غير دقيق، وبالتالي لا يمكن تحديد موقعها بسهولة في مصادرها. فضلًا، ساهم بتحسينها بعزو الاستشهادات إلى المصادر في متن المقالة. (يوليو 2018) عبد الرحمن بن كيسان معلومات شخصية تاريخ الميلاد 201 هـ / 816م [1] تاريخ الوفاة 279 هـ /...