Problem dealing with big numbers.

조회 수: 7 (최근 30일)
abid
abid 2017년 12월 17일
편집: abid 2017년 12월 18일
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
John D'Errico
John D'Errico 2017년 12월 17일
편집: John D'Errico 2017년 12월 17일
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 2017년 12월 18일
편집: abid 2017년 12월 18일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by