How to find the last digits of 2^2009 [closed] - algorithm

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I was using this formula to calculate last m digits of 2^n.
pow=2+(n-m)%(4*5^(m-1))
ans =(2^pow)%(10^m)**
But this is not working for n=2009 and m=3.
Suggest any error in my calculation or a better formula if there is.

I don't understand what your formula is doing, but the simplest way is to calculate (2^2009)%(10^m) . Here is a pseudo code to find (x^y)%mod in O(log y). Put x=2, y=2009 and mod=10^m
power(x,y)
{
if( y == 0)
return 1
temp = power(x, y/2)
if (y%2 == 0)
return (temp*temp)%mod
else
return ((x*temp%mod)*temp)%mod
}

Related

What type of algorithm would this be called? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What type of algorithm would this be called? Is it any sorting algorithm or bruteforce algorithm?
boolean isPrime = true;
for(int i=2; i<=number/2; i++){
if(number%i == 0){
isPrime = false;
break;
}
}
if(isPrime){
System.out.println("Prime");
}
System.out.println("Not Prime");
The algorithm apparently tests the variable number for primality by using all smaller numbers as potential factors; the approach could be considered brute-force.

Generate all possible combinations of 5 binary numbers so that there sum is less or equal to 3 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How can I generate a matrix in Matlab that has 5 rows and some specific number of columns and the elements may only be binary numbers and the column sum has to be less or equal to 3?
Some possibilites without loops:
Using strings:
D = 5;
S = 3;
numbers = str2mat(dec2bin(0:2^D-1))-'0';
numbers = numbers(sum(numbers,2)<=S,:);
Using combinatorial numbers, one line:
numbers = [zeros(1,D); cell2mat(arrayfun(#(s) fliplr(full(sparse((1:nchoosek(D,s)).'*ones(1,s), nchoosek(1:D,s), 1))), 0:S, 'uni', 0).')];
How about this: The maximum binary number, that you can represent by 5bit is 2^5-1 = 31 and skip through these to find the ones with sum of digits <= 3.
Something like
n = 1:1:31;
for ii = 1:length(ii)
bin = dec2bin(ii)
digitSum = 0
for d = 1:length(bin)
digitSum = digitSum + str2num(bin(d))
end
if (digitSum <= 3)
%store results
end
end
Here is a vecotorized solution to provide all occurences efficiently:
Bstr =dec2bin(1:31);
Bstr(sum(dec2bin(0:31),2)<=sum('00111'),:)=='1'
Inspired by the solution of #pyStarter

How to calculate the sum of(1+a%m+a^2%m……+a^n%m) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to calculate the sum of (1+a%m+a^2%m……+a^n%m) where
m=k!, 1<=k<=12, n<=10^18. How to Calculate this sum.
Using computer and the time limit is 3 sec.
Sorry about my mistake
1+a+a^2+...+a^n = (1+a+a^2+...+a^n)*(1-a)/(1-a) =
= (1 - a^(n+1))/(1-a)
In other words, your expression can be computed as:
(1 - a^(n+1))/(1-a) % m
Or, in programmatic form,
fmod((1-pow(a,n+1))/(1-a), m)
sum = 0;
i = 0;
while(i <= n){
sum = sum + math.pow(a,i);
i++;
}
result = sum % m;

matrix expression simplify [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
i am wondering if it is possible to simplify:
T*V + V*T // V = V^(t) symmetric
where both operands are matrixes
I don't think this is possible due to the following considerations:
If we multiply two matrices A and T, where A is symmetric (i.e. A(i,j) = A(j,i)), we have the following:
For A*T we have that the item in row z and column s is computed as:
__n__
\
/ A(z,i)*T(i,s)
-----
i=1
For the other way around, T*A, we get for row z, column s:
__n__ __n__
\ \
/ T(z,i)*A(i,s) = / T(z,i)*A(s,i)
----- -----
i=1 i=1
So, as long as we do not know anything about the entries T(i,j) in T, I think we can not say how these sums relate to each other.

How to calculate the inverse factorial of a real number? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
Is there some way to calculate the inverse factorials of real numbers?
For example - 1.5 ! = 1.32934039
Is there some way to obtain 1.5 back if I have the value 1.32934039?
I am trying
http://www.wolframalpha.com/input/?i=Gamma^(-1)[1.32934039]
but that is a fail.
Using wolframalpha.com, you can ask for
Solve[Gamma[x+1]==1.32934039,x]
As mentioned in the comments, Gamma does not have a unique inverse. True even when you are solving for a conventional factorial, e.g.
Solve[Gamma[x+1]==6,x]
yields several answers, of which one is 3.
Instead of using Gamma[] in WolframAlpha, you can also use Factorial[]:
Solve[Factorial[x]==6,x]
Solve[Factorial[x]==1.32934039,x]
David Cantrell gives a good approximation of Γ-1(n) on this page:
k = the positive zero of the digamma function, approximately 1.461632
c = Sqrt(2*pi)/e - Γ(k), approximately 0.036534
L(x) = ln((x+c)/Sqrt(2*pi))
W(x) = Lambert W function
ApproxInvGamma(x) = L(x) / W(L(x) / e) + 1/2
For integers you can do:
i = 2
n = someNum
while (n != 1):
n /= i
i += 1
return (i==1 ? i : None)
The factorial for real numbers has no inverse. You say that "each function must have an inverse". That is incorrect. Consider the constant function f(x)=0. What is f^-1(42)? For a function to be inverse it must be both an injection and a surjection.

Resources