Reputation:
I use factor(n)
in matlab but it don't work for numbers like ((10^11-1)/11)
- what can I do? My source is here.
m=input('Enter your number: ');
i=0;
while (i<m)
if(isprime(i))
% sum=factor((10^(i-1)-1));
sum=factor((10^(i-1)-1)/i);
disp(sum);
end
i =i+1;
end
but for large n it returns errors!!
>> FactorGen Enter your number: 45 3 3 3 3 11 3 3 11 101 3 3 3 7 11 13 37 3 3 11 41 271 9091 3 3 3 7 11 13 37 101 9901 Error using factor (line 26) When n is single or double, its maximum allowed value is FLINTMAX. Error in FactorGen (line 7) sum=factor((10^(i-1)-1));
I want the function factor((10^(i-1)-1))
to work for same number. How can I solve my problem?
Upvotes: 2
Views: 407
Reputation: 1345
I think this can be partially alleviated by converting your large number into uint64
format. For R2014b maximum integer that can be handled is:
n = intmax('uint64')
n = 1.8447e+19
While the maximum double
that can be handled is:
n = flintmax('double')
n = 9.0072e+15
This can be verified by simple example. Let's use factor
on the number larger than flintmax
. First, try double
:
factor(10^16)
Error using factor (line 26)
When n is single or double, its maximum allowed value is FLINTMAX.
Now, we try uint64
:
factor(uint64(10^16))
ans = 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
So, using factor(uint64(x))
increases your limit by several orders. Indeed, there's no sense in using double
format for factor
function.
By the way, since using factor
on large numbers may freeze your system, I suggest using this function:
function f = Factorize(n)
i = 0;
while mod(n,2) == 0
i = i + 1;
n = n/2;
f(i) = 2;
disp([2,n])
end
q = round(sqrt(double(n)));
q = q + 1 - mod(q,2);
for j = 3:2:q
while mod(n,j) == 0
i = i + 1;
f(i) = j;
n = n/j;
end
end
if n > 2;
i = i + 1;
f(i) = n;
end
It is much faster for large numbers and does not overload the system at large n Hope that helps
Upvotes: 1