[{"data":1,"prerenderedAt":711},["ShallowReactive",2],{"content-hero-\u002Fcodesnippets\u002Fcsharp\u002Ffast-exponentiation":3,"content-query-LZ9n2ObuiS":6},{"title":4,"imageUrl":5},"Fast exponentiation - C# implementation","\u002Fimages\u002Fthumbnails\u002Flogo_maximilien_zakowski_csharp.gif",{"_path":7,"_dir":8,"_draft":9,"_partial":9,"_locale":10,"title":4,"description":11,"imageUrl":5,"body":12,"_type":705,"_id":706,"_source":707,"_file":708,"_stem":709,"_extension":710},"\u002Fcodesnippets\u002Fcsharp\u002Ffast-exponentiation","csharp",false,"","Recursive and iterative fast exponentiation using bit tricks for fewer multiplications.",{"type":13,"children":14,"toc":701},"root",[15,23,28,33,38,43,48,53,60,394,400,695],{"type":16,"tag":17,"props":18,"children":19},"element","p",{},[20],{"type":21,"value":22},"text","When you want to power very fast a number in order to meet some performance constraints the standard math power algorithm implementation can be overwhelmed.",{"type":16,"tag":17,"props":24,"children":25},{},[26],{"type":21,"value":27},"Fast exponentiation algorithm take advantage of the computer binary memory structure in order to avoid complexe arithmetic computation.",{"type":16,"tag":17,"props":29,"children":30},{},[31],{"type":21,"value":32},"This algorithm allows you to raise any number to a natural power for a reduced number of multiplications.",{"type":16,"tag":17,"props":34,"children":35},{},[36],{"type":21,"value":37},"For any number x and even degree n, the identity holds:",{"type":16,"tag":17,"props":39,"children":40},{},[41],{"type":21,"value":42},"x^n = (x^(n\u002F2))^2 = x^(n\u002F2) · x^(n\u002F2)",{"type":16,"tag":17,"props":44,"children":45},{},[46],{"type":21,"value":47},"For the case of odd degree, it is enough to lower it by one:",{"type":16,"tag":17,"props":49,"children":50},{},[51],{"type":21,"value":52},"x^n = x^(n−1) · x, while (n − 1) is an even number.",{"type":16,"tag":54,"props":55,"children":57},"h3",{"id":56},"fast-exponentiation-recursive-implementation-beware-of-stack-overflow",[58],{"type":21,"value":59},"Fast exponentiation – recursive implementation (beware of stack overflow)",{"type":16,"tag":61,"props":62,"children":65},"pre",{"className":63,"code":64,"language":8,"meta":10,"style":10},"language-csharp shiki shiki-themes github-light github-dark","static long RecursivePower(long x, int n)\n{\n    if (n == 0) return 1; \u002F\u002F in any case Power(x,0) give 1\n    if ((n & 1) == 0) \u002F\u002F Bit comparison to check if is an even number\n    {\n        var p = RecursivePower(x, n >> 1); \u002F\u002F Equivalent to Power(x, n\u002F2)\n        return p * p;\n    }\n    else\n    {\n        return x * RecursivePower(x, n - 1);\n    }\n}\n",[66],{"type":16,"tag":67,"props":68,"children":69},"code",{"__ignoreMap":10},[70,129,138,189,232,241,288,312,321,330,338,377,385],{"type":16,"tag":71,"props":72,"children":75},"span",{"class":73,"line":74},"line",1,[76,82,87,93,99,104,109,114,119,124],{"type":16,"tag":71,"props":77,"children":79},{"style":78},"--shiki-default:#D73A49;--shiki-dark:#F97583",[80],{"type":21,"value":81},"static",{"type":16,"tag":71,"props":83,"children":84},{"style":78},[85],{"type":21,"value":86}," long",{"type":16,"tag":71,"props":88,"children":90},{"style":89},"--shiki-default:#6F42C1;--shiki-dark:#B392F0",[91],{"type":21,"value":92}," RecursivePower",{"type":16,"tag":71,"props":94,"children":96},{"style":95},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[97],{"type":21,"value":98},"(",{"type":16,"tag":71,"props":100,"children":101},{"style":78},[102],{"type":21,"value":103},"long",{"type":16,"tag":71,"props":105,"children":106},{"style":89},[107],{"type":21,"value":108}," x",{"type":16,"tag":71,"props":110,"children":111},{"style":95},[112],{"type":21,"value":113},", ",{"type":16,"tag":71,"props":115,"children":116},{"style":78},[117],{"type":21,"value":118},"int",{"type":16,"tag":71,"props":120,"children":121},{"style":89},[122],{"type":21,"value":123}," n",{"type":16,"tag":71,"props":125,"children":126},{"style":95},[127],{"type":21,"value":128},")\n",{"type":16,"tag":71,"props":130,"children":132},{"class":73,"line":131},2,[133],{"type":16,"tag":71,"props":134,"children":135},{"style":95},[136],{"type":21,"value":137},"{\n",{"type":16,"tag":71,"props":139,"children":141},{"class":73,"line":140},3,[142,147,152,157,163,168,173,178,183],{"type":16,"tag":71,"props":143,"children":144},{"style":78},[145],{"type":21,"value":146},"    if",{"type":16,"tag":71,"props":148,"children":149},{"style":95},[150],{"type":21,"value":151}," (n ",{"type":16,"tag":71,"props":153,"children":154},{"style":78},[155],{"type":21,"value":156},"==",{"type":16,"tag":71,"props":158,"children":160},{"style":159},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[161],{"type":21,"value":162}," 0",{"type":16,"tag":71,"props":164,"children":165},{"style":95},[166],{"type":21,"value":167},") ",{"type":16,"tag":71,"props":169,"children":170},{"style":78},[171],{"type":21,"value":172},"return",{"type":16,"tag":71,"props":174,"children":175},{"style":159},[176],{"type":21,"value":177}," 1",{"type":16,"tag":71,"props":179,"children":180},{"style":95},[181],{"type":21,"value":182},"; ",{"type":16,"tag":71,"props":184,"children":186},{"style":185},"--shiki-default:#6A737D;--shiki-dark:#6A737D",[187],{"type":21,"value":188},"\u002F\u002F in any case Power(x,0) give 1\n",{"type":16,"tag":71,"props":190,"children":192},{"class":73,"line":191},4,[193,197,202,207,211,215,219,223,227],{"type":16,"tag":71,"props":194,"children":195},{"style":78},[196],{"type":21,"value":146},{"type":16,"tag":71,"props":198,"children":199},{"style":95},[200],{"type":21,"value":201}," ((n ",{"type":16,"tag":71,"props":203,"children":204},{"style":78},[205],{"type":21,"value":206},"&",{"type":16,"tag":71,"props":208,"children":209},{"style":159},[210],{"type":21,"value":177},{"type":16,"tag":71,"props":212,"children":213},{"style":95},[214],{"type":21,"value":167},{"type":16,"tag":71,"props":216,"children":217},{"style":78},[218],{"type":21,"value":156},{"type":16,"tag":71,"props":220,"children":221},{"style":159},[222],{"type":21,"value":162},{"type":16,"tag":71,"props":224,"children":225},{"style":95},[226],{"type":21,"value":167},{"type":16,"tag":71,"props":228,"children":229},{"style":185},[230],{"type":21,"value":231},"\u002F\u002F Bit comparison to check if is an even number\n",{"type":16,"tag":71,"props":233,"children":235},{"class":73,"line":234},5,[236],{"type":16,"tag":71,"props":237,"children":238},{"style":95},[239],{"type":21,"value":240},"    {\n",{"type":16,"tag":71,"props":242,"children":244},{"class":73,"line":243},6,[245,250,255,260,264,269,274,278,283],{"type":16,"tag":71,"props":246,"children":247},{"style":78},[248],{"type":21,"value":249},"        var",{"type":16,"tag":71,"props":251,"children":252},{"style":89},[253],{"type":21,"value":254}," p",{"type":16,"tag":71,"props":256,"children":257},{"style":78},[258],{"type":21,"value":259}," =",{"type":16,"tag":71,"props":261,"children":262},{"style":89},[263],{"type":21,"value":92},{"type":16,"tag":71,"props":265,"children":266},{"style":95},[267],{"type":21,"value":268},"(x, n ",{"type":16,"tag":71,"props":270,"children":271},{"style":78},[272],{"type":21,"value":273},">>",{"type":16,"tag":71,"props":275,"children":276},{"style":159},[277],{"type":21,"value":177},{"type":16,"tag":71,"props":279,"children":280},{"style":95},[281],{"type":21,"value":282},"); ",{"type":16,"tag":71,"props":284,"children":285},{"style":185},[286],{"type":21,"value":287},"\u002F\u002F Equivalent to Power(x, n\u002F2)\n",{"type":16,"tag":71,"props":289,"children":291},{"class":73,"line":290},7,[292,297,302,307],{"type":16,"tag":71,"props":293,"children":294},{"style":78},[295],{"type":21,"value":296},"        return",{"type":16,"tag":71,"props":298,"children":299},{"style":95},[300],{"type":21,"value":301}," p ",{"type":16,"tag":71,"props":303,"children":304},{"style":78},[305],{"type":21,"value":306},"*",{"type":16,"tag":71,"props":308,"children":309},{"style":95},[310],{"type":21,"value":311}," p;\n",{"type":16,"tag":71,"props":313,"children":315},{"class":73,"line":314},8,[316],{"type":16,"tag":71,"props":317,"children":318},{"style":95},[319],{"type":21,"value":320},"    }\n",{"type":16,"tag":71,"props":322,"children":324},{"class":73,"line":323},9,[325],{"type":16,"tag":71,"props":326,"children":327},{"style":78},[328],{"type":21,"value":329},"    else\n",{"type":16,"tag":71,"props":331,"children":333},{"class":73,"line":332},10,[334],{"type":16,"tag":71,"props":335,"children":336},{"style":95},[337],{"type":21,"value":240},{"type":16,"tag":71,"props":339,"children":341},{"class":73,"line":340},11,[342,346,351,355,359,363,368,372],{"type":16,"tag":71,"props":343,"children":344},{"style":78},[345],{"type":21,"value":296},{"type":16,"tag":71,"props":347,"children":348},{"style":95},[349],{"type":21,"value":350}," x ",{"type":16,"tag":71,"props":352,"children":353},{"style":78},[354],{"type":21,"value":306},{"type":16,"tag":71,"props":356,"children":357},{"style":89},[358],{"type":21,"value":92},{"type":16,"tag":71,"props":360,"children":361},{"style":95},[362],{"type":21,"value":268},{"type":16,"tag":71,"props":364,"children":365},{"style":78},[366],{"type":21,"value":367},"-",{"type":16,"tag":71,"props":369,"children":370},{"style":159},[371],{"type":21,"value":177},{"type":16,"tag":71,"props":373,"children":374},{"style":95},[375],{"type":21,"value":376},");\n",{"type":16,"tag":71,"props":378,"children":380},{"class":73,"line":379},12,[381],{"type":16,"tag":71,"props":382,"children":383},{"style":95},[384],{"type":21,"value":320},{"type":16,"tag":71,"props":386,"children":388},{"class":73,"line":387},13,[389],{"type":16,"tag":71,"props":390,"children":391},{"style":95},[392],{"type":21,"value":393},"}\n",{"type":16,"tag":54,"props":395,"children":397},{"id":396},"fast-exponentiation-iterative-implementation",[398],{"type":21,"value":399},"Fast exponentiation – Iterative implementation",{"type":16,"tag":61,"props":401,"children":403},{"className":63,"code":402,"language":8,"meta":10,"style":10},"static long IterativePower(long x, int n)\n{\n    var result = 1L;\n    while (n > 0)\n    {\n        if ((n & 1) == 0) \u002F\u002F Bit comparison to check if is an even number\n        {\n            x *= x;\n            n >>= 1;\n        }\n        else\n        {\n            result *= x;\n            --n;\n        }\n    }\n    return result;\n}\n",[404],{"type":16,"tag":67,"props":405,"children":406},{"__ignoreMap":10},[407,451,458,485,510,517,557,565,583,604,612,620,627,643,657,665,673,687],{"type":16,"tag":71,"props":408,"children":409},{"class":73,"line":74},[410,414,418,423,427,431,435,439,443,447],{"type":16,"tag":71,"props":411,"children":412},{"style":78},[413],{"type":21,"value":81},{"type":16,"tag":71,"props":415,"children":416},{"style":78},[417],{"type":21,"value":86},{"type":16,"tag":71,"props":419,"children":420},{"style":89},[421],{"type":21,"value":422}," IterativePower",{"type":16,"tag":71,"props":424,"children":425},{"style":95},[426],{"type":21,"value":98},{"type":16,"tag":71,"props":428,"children":429},{"style":78},[430],{"type":21,"value":103},{"type":16,"tag":71,"props":432,"children":433},{"style":89},[434],{"type":21,"value":108},{"type":16,"tag":71,"props":436,"children":437},{"style":95},[438],{"type":21,"value":113},{"type":16,"tag":71,"props":440,"children":441},{"style":78},[442],{"type":21,"value":118},{"type":16,"tag":71,"props":444,"children":445},{"style":89},[446],{"type":21,"value":123},{"type":16,"tag":71,"props":448,"children":449},{"style":95},[450],{"type":21,"value":128},{"type":16,"tag":71,"props":452,"children":453},{"class":73,"line":131},[454],{"type":16,"tag":71,"props":455,"children":456},{"style":95},[457],{"type":21,"value":137},{"type":16,"tag":71,"props":459,"children":460},{"class":73,"line":140},[461,466,471,475,480],{"type":16,"tag":71,"props":462,"children":463},{"style":78},[464],{"type":21,"value":465},"    var",{"type":16,"tag":71,"props":467,"children":468},{"style":89},[469],{"type":21,"value":470}," result",{"type":16,"tag":71,"props":472,"children":473},{"style":78},[474],{"type":21,"value":259},{"type":16,"tag":71,"props":476,"children":477},{"style":159},[478],{"type":21,"value":479}," 1L",{"type":16,"tag":71,"props":481,"children":482},{"style":95},[483],{"type":21,"value":484},";\n",{"type":16,"tag":71,"props":486,"children":487},{"class":73,"line":191},[488,493,497,502,506],{"type":16,"tag":71,"props":489,"children":490},{"style":78},[491],{"type":21,"value":492},"    while",{"type":16,"tag":71,"props":494,"children":495},{"style":95},[496],{"type":21,"value":151},{"type":16,"tag":71,"props":498,"children":499},{"style":78},[500],{"type":21,"value":501},">",{"type":16,"tag":71,"props":503,"children":504},{"style":159},[505],{"type":21,"value":162},{"type":16,"tag":71,"props":507,"children":508},{"style":95},[509],{"type":21,"value":128},{"type":16,"tag":71,"props":511,"children":512},{"class":73,"line":234},[513],{"type":16,"tag":71,"props":514,"children":515},{"style":95},[516],{"type":21,"value":240},{"type":16,"tag":71,"props":518,"children":519},{"class":73,"line":243},[520,525,529,533,537,541,545,549,553],{"type":16,"tag":71,"props":521,"children":522},{"style":78},[523],{"type":21,"value":524},"        if",{"type":16,"tag":71,"props":526,"children":527},{"style":95},[528],{"type":21,"value":201},{"type":16,"tag":71,"props":530,"children":531},{"style":78},[532],{"type":21,"value":206},{"type":16,"tag":71,"props":534,"children":535},{"style":159},[536],{"type":21,"value":177},{"type":16,"tag":71,"props":538,"children":539},{"style":95},[540],{"type":21,"value":167},{"type":16,"tag":71,"props":542,"children":543},{"style":78},[544],{"type":21,"value":156},{"type":16,"tag":71,"props":546,"children":547},{"style":159},[548],{"type":21,"value":162},{"type":16,"tag":71,"props":550,"children":551},{"style":95},[552],{"type":21,"value":167},{"type":16,"tag":71,"props":554,"children":555},{"style":185},[556],{"type":21,"value":231},{"type":16,"tag":71,"props":558,"children":559},{"class":73,"line":290},[560],{"type":16,"tag":71,"props":561,"children":562},{"style":95},[563],{"type":21,"value":564},"        {\n",{"type":16,"tag":71,"props":566,"children":567},{"class":73,"line":314},[568,573,578],{"type":16,"tag":71,"props":569,"children":570},{"style":95},[571],{"type":21,"value":572},"            x ",{"type":16,"tag":71,"props":574,"children":575},{"style":78},[576],{"type":21,"value":577},"*=",{"type":16,"tag":71,"props":579,"children":580},{"style":95},[581],{"type":21,"value":582}," x;\n",{"type":16,"tag":71,"props":584,"children":585},{"class":73,"line":323},[586,591,596,600],{"type":16,"tag":71,"props":587,"children":588},{"style":95},[589],{"type":21,"value":590},"            n ",{"type":16,"tag":71,"props":592,"children":593},{"style":78},[594],{"type":21,"value":595},">>=",{"type":16,"tag":71,"props":597,"children":598},{"style":159},[599],{"type":21,"value":177},{"type":16,"tag":71,"props":601,"children":602},{"style":95},[603],{"type":21,"value":484},{"type":16,"tag":71,"props":605,"children":606},{"class":73,"line":332},[607],{"type":16,"tag":71,"props":608,"children":609},{"style":95},[610],{"type":21,"value":611},"        }\n",{"type":16,"tag":71,"props":613,"children":614},{"class":73,"line":340},[615],{"type":16,"tag":71,"props":616,"children":617},{"style":78},[618],{"type":21,"value":619},"        else\n",{"type":16,"tag":71,"props":621,"children":622},{"class":73,"line":379},[623],{"type":16,"tag":71,"props":624,"children":625},{"style":95},[626],{"type":21,"value":564},{"type":16,"tag":71,"props":628,"children":629},{"class":73,"line":387},[630,635,639],{"type":16,"tag":71,"props":631,"children":632},{"style":95},[633],{"type":21,"value":634},"            result ",{"type":16,"tag":71,"props":636,"children":637},{"style":78},[638],{"type":21,"value":577},{"type":16,"tag":71,"props":640,"children":641},{"style":95},[642],{"type":21,"value":582},{"type":16,"tag":71,"props":644,"children":646},{"class":73,"line":645},14,[647,652],{"type":16,"tag":71,"props":648,"children":649},{"style":78},[650],{"type":21,"value":651},"            --",{"type":16,"tag":71,"props":653,"children":654},{"style":95},[655],{"type":21,"value":656},"n;\n",{"type":16,"tag":71,"props":658,"children":660},{"class":73,"line":659},15,[661],{"type":16,"tag":71,"props":662,"children":663},{"style":95},[664],{"type":21,"value":611},{"type":16,"tag":71,"props":666,"children":668},{"class":73,"line":667},16,[669],{"type":16,"tag":71,"props":670,"children":671},{"style":95},[672],{"type":21,"value":320},{"type":16,"tag":71,"props":674,"children":676},{"class":73,"line":675},17,[677,682],{"type":16,"tag":71,"props":678,"children":679},{"style":78},[680],{"type":21,"value":681},"    return",{"type":16,"tag":71,"props":683,"children":684},{"style":95},[685],{"type":21,"value":686}," result;\n",{"type":16,"tag":71,"props":688,"children":690},{"class":73,"line":689},18,[691],{"type":16,"tag":71,"props":692,"children":693},{"style":95},[694],{"type":21,"value":393},{"type":16,"tag":696,"props":697,"children":698},"style",{},[699],{"type":21,"value":700},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":10,"searchDepth":131,"depth":131,"links":702},[703,704],{"id":56,"depth":140,"text":59},{"id":396,"depth":140,"text":399},"markdown","content:codesnippets:csharp:fast-exponentiation.md","content","codesnippets\u002Fcsharp\u002Ffast-exponentiation.md","codesnippets\u002Fcsharp\u002Ffast-exponentiation","md",1779013873258]