MATLAB Answers

Ken
0

convert inf value to integer value

Asked by Ken
on 3 Apr 2014
Latest activity Answered by Ken
on 7 Apr 2014
I am making operation in matlab, such as mod((11239388^375) , 751 ). However the result are NaN because 11239388^375 results inf. Is there anyway to make 11239388^375 results in integer? Thanks :D

  0 Comments

Sign in to comment.

5 Answers

Answer by Nitin
on 4 Apr 2014

Download the powermod function here from John D'Errico
Then run the following command:
powermod(11239388,375,751) % Compute mod(a^d,n)

  0 Comments

Sign in to comment.


Answer by Walter Roberson
on 4 Apr 2014

b = 11239388;
e = 375;
m = 751;
feval(symengine, 'powermod', b, e, m)
See also John's contribution as mentioned by Nitin, and see http://www.mathworks.com/matlabcentral/fileexchange/38516-powermod

  0 Comments

Sign in to comment.


Answer by Ken
on 5 Apr 2014

Thanks Nitin and Walter :) I'll try that. However my program often run into busy state, either i set the breakpoint or not. Then I try part of the program, e.g. :
e=1;
p=753;
s=(p-1)/(2^e);
while mod(s,2) == 0
e=e+1;
s=(p-1)/(2^e);
end
When I put a breakpoint at "end", then I keep continue until I get the result e = 4 and s = 47. But when I just run the program without putting any breakpoint, that program went to "busy" state in the command window. Why I should put breakpoint in order to make the program run? THX

  0 Comments

Sign in to comment.


Answer by Roger Stafford on 5 Apr 2014
Edited by Roger Stafford on 5 Apr 2014

John's function 'powermod' is a very efficient method of solving your problem, but it is possible to solve it in such a way that the solution is eminently clear to you without taking 'powermod' on trust.
To begin with, if you do p = mod(11239388,751), you will know that
11239388 = p + some integral multiple of 751
where p is a certain integer less than 751. Therefore
11239388^375 = (p + that integral multiple of 751)^375
= p^375 + some other larger multiple of 751
by the binomial theorem. Hence the problem is reduced to finding just the quantity mod(p^375,751), which is a lot easier.
You can solve that by starting with 1 and repeatedly multiplying it by p, then taking its mod(~,751), in a for-loop 375 times. Each time the 'mod' operation will keep the number within the necessary upper limit of accuracy for 'double' values before proceeding with the next multiplication. At the end you will have your desired answer.
(Actually in this particular problem you can no doubt arrive at an answer faster than than you could with John's 'powermod' if you only do this last operation 15 instead of 375 times and take a careful look at the answer, remembering that 375 is a multiple of 15.)

  0 Comments

Sign in to comment.


Answer by Ken
on 7 Apr 2014

thanks guys it's working :D However, part of my program still need the value of 11239388^375 to become an integer . Is there anyway to make 11239388^375 results in integer instead of inf?

  0 Comments

Sign in to comment.