Problem dealing with big numbers.

7 views (last 30 days)
abid
abid on 17 Dec 2017
Edited: abid on 18 Dec 2017
In my project , I am trying to find nth permutation of a set of 50 symbols [1:50]. theoretically this should have 50! permutations in total, I am using factorial number system to find the nth permutation directly . when I run it for some smaller n it works fine but when I am trying to find some higher permutation such as last permutation i.e. (50!-1)th perm, (50!=30414093201713378043612608166064768844377641568960512000000000000), the output should be exactly flip version(50,49,48,47...3,2,1) of 0th perm (1,2,3,4,5,....47,48,49,50) but answer what I am getting is " 49, 48, 47, 46, 45, 44, 43, 42, 41, 37, 39, 1, 27 9 9 17 18 4 7 14 28 0 21 1 15 12 6 10 0 11 17
32 50
11 9 12 6 11 7 3 10 2 1 6 5 1 0 2 2 2 0 0
can anybody identify why I am not getting right answer ? for big numbers I used vpa , digits(300) but still problem persists.
  2 Comments
John D'Errico
John D'Errico on 17 Dec 2017
Edited: John D'Errico on 17 Dec 2017
What you don't show is what you did. It is not obvious that you are not using some double precision computations which will of course fail for numbers this large. Even if you used vpa, that still does not preclude problems. For example,
digits 300
vpa(factorial(50))
ans =
30414093201713375576366966406747986832057064836514787179557289984.0
will generate garbage, because factorial(50) will not be the number you think it is since it was computed using double precision arithmetic. As opposed to that, we have:
factorial(sym(50))
ans =
30414093201713378043612608166064768844377641568960512000000000000
So if you want serious help, you need to show what you are actually doing.
abid
abid on 18 Dec 2017
Edited: abid on 18 Dec 2017
Thanks john , you have rightly pointed out . I did the same as you mentioned i.e. digits 300 vpa(factorial(50)). Now I got the desired answer . Great help sir john.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!