exponentiation

Does pow() work for int data type in C? [duplicate]

泄露秘密 提交于 2019-11-27 22:59:05
This question already has an answer here: Strange behaviour of the pow function 5 answers I was simply writing a program to calculate the power of an integer. But the output was not as expected. It worked for all the integer numbers except for the power of 5. My code is: #include <stdio.h> #include <math.h> int main(void) { int a,b; printf("Enter the number."); scanf("\n%d",&a); b=pow(a,2); printf("\n%d",b); } The output is something like this: "Enter the number. 2 4 "Enter the number. 5 24 "Enter the number. 4 16 "Enter the number. 10 99 Can't we use pow() function for int data type??

Efficient way to compute p^q (exponentiation), where q is an integer

时光毁灭记忆、已成空白 提交于 2019-11-27 15:52:26
问题 What is an efficient way to compute p q , where q is an integer? 回答1: Exponentiation by squaring uses only O(lg q ) multiplications. template <typename T> T expt(T p, unsigned q) { T r(1); while (q != 0) { if (q % 2 == 1) { // q is odd r *= p; q--; } p *= p; q /= 2; } return r; } This should work on any monoid ( T , operator* ) where a T constructed from 1 is the identity element. That includes all numeric types. Extending this to signed q is easy: just divide one by the result of the above

How do you do *integer* exponentiation in C#?

偶尔善良 提交于 2019-11-27 08:33:14
The built-in Math.Pow() function in .NET raises a double base to a double exponent and returns a double result. What's the best way to do the same with integers? Added: It seems that one can just cast Math.Pow() result to (int), but will this always produce the correct number and no rounding errors? A pretty fast one might be something like this: int IntPow(int x, uint pow) { int ret = 1; while ( pow != 0 ) { if ( (pow & 1) == 1 ) ret *= x; x *= x; pow >>= 1; } return ret; } Note that this does not allow negative powers. I'll leave that as an exercise to you. :) Added: Oh yes, almost forgot -

Prolog predicate - infinite loop

ε祈祈猫儿з 提交于 2019-11-27 07:54:31
问题 I need to create a Prolog predicate for power of 2, with the natural numbers. Natural numbers are: 0, s(0), s(s(0)) ans so on.. For example: ?- pow2(s(0),P). P = s(s(0)); false. ?- pow2(P,s(s(0))). P = s(0); false. This is my code: times2(X,Y) :- add(X,X,Y). pow2(0,s(0)). pow2(s(N),Y) :- pow2(N,Z), times2(Z,Y). And it works perfectly with the first example, but enters an infinite loop in the second.. How can I fix this? 回答1: This happends because the of evaluation order of pow2. If you switch

I want to generate the nth term of the sequence 1,3,8,22,60 ,164 in Order(1) or order of (nlogn)

自作多情 提交于 2019-11-27 05:33:27
This sequence satisfies a(n+2) = 2 a(n+1) + 2 a(n). and also a(n)=[(1+sqrt(3))^(n+2)-(1-sqrt(3))^(n+2)]/(4sqrt(3)). I am using C++ for me n can vary from 1 to 10^ 9. I need the answers modulo (10^9)+7 But speed here is very important My code with formula1 is slow for numbers > 10^7 #include <iostream> #define big unsigned long long int #include<stdlib.h> int ans[100000001]={0}; big m =1000000007; using namespace std; int main() { //cout << "Hello world!" << endl; big t,n; cin>>t; big a,b,c; a=1; b=3; c=8; ans[0]=0; ans[1]=1; ans[2]=3; ans[3]=8; for(big i=3;i<=100000000;i++) { ans[i]=(((((ans[i

Why is exponentiation applied right to left?

早过忘川 提交于 2019-11-26 23:32:01
问题 I am reading an Intro to Python textbook and came across this line: Operators on the same row have equal precedence and are applied left to right, except for exponentiation, which is applied right to left. I understand most of this, but I do not understand why they say exponentiation is applied right to left. They do not provide any examples either. Also, am I allowed to ask general questions like this, or are only problem solving questions preferred? 回答1: The ** operator follows normal

Raising to power in PHP

心不动则不痛 提交于 2019-11-26 17:21:19
问题 Well, i need to do some calculations in PHP script. And i have one expression that behaves wrong. echo 10^(-.01); Outputs 10 echo 1 / (10^(.01)); Outputs 0 echo bcpow('10', '-0.01') . '<br/>'; Outputs 1 echo bcdiv('1', bcpow('10', '0.01')); Outputs 1.000.... I'm using bcscale(100) for BCMath calculations. Excel and Wolfram Mathematica give answer ~0,977237. Any suggestions? 回答1: The caret is the bit-wise XOR operator in PHP. You need to use pow() for integers. 回答2: PHP 5.6 finally introduced

Modular Exponentiation for high numbers in C++

孤人 提交于 2019-11-26 14:22:52
问题 So I've been working recently on an implementation of the Miller-Rabin primality test. I am limiting it to a scope of all 32-bit numbers, because this is a just-for-fun project that I am doing to familiarize myself with c++, and I don't want to have to work with anything 64-bits for awhile. An added bonus is that the algorithm is deterministic for all 32-bit numbers, so I can significantly increase efficiency because I know exactly what witnesses to test for. So for low numbers, the algorithm

How do you do *integer* exponentiation in C#?

匆匆过客 提交于 2019-11-26 14:11:27
问题 The built-in Math.Pow() function in .NET raises a double base to a double exponent and returns a double result. What's the best way to do the same with integers? Added: It seems that one can just cast Math.Pow() result to (int), but will this always produce the correct number and no rounding errors? 回答1: A pretty fast one might be something like this: int IntPow(int x, uint pow) { int ret = 1; while ( pow != 0 ) { if ( (pow & 1) == 1 ) ret *= x; x *= x; pow >>= 1; } return ret; } Note that

I want to generate the nth term of the sequence 1,3,8,22,60 ,164 in Order(1) or order of (nlogn)

余生颓废 提交于 2019-11-26 11:37:41
问题 This sequence satisfies a(n+2) = 2 a(n+1) + 2 a(n). and also a(n)=[(1+sqrt(3))^(n+2)-(1-sqrt(3))^(n+2)]/(4sqrt(3)). I am using C++ for me n can vary from 1 to 10^ 9. I need the answers modulo (10^9)+7 But speed here is very important My code with formula1 is slow for numbers > 10^7 #include <iostream> #define big unsigned long long int #include<stdlib.h> int ans[100000001]={0}; big m =1000000007; using namespace std; int main() { //cout << \"Hello world!\" << endl; big t,n; cin>>t; big a,b,c;