Integer check with a symbolic variable

조회 수: 3 (최근 30일)
Yuechuan Chen
Yuechuan Chen 2019년 12월 12일
댓글: Yuechuan Chen 2019년 12월 13일
So I have:
s=sym('10517669');
p=nextprime(8*(S^4));
if float((p-1)/2)==(p-1)/2
...
end
I know float only works for single&double-precision, but p is so large I have to use the symbolic toolbox because it don't want to lose precision.
How could I resolve this?
THANKS
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2019년 12월 12일
p is accurately represented using sym object in your code. You know p is an integer from theory. What do you try to do? double(p) or single(p) can't represent p without precision loss.
Yuechuan Chen
Yuechuan Chen 2019년 12월 12일
편집: Yuechuan Chen 2019년 12월 12일
Oh sorry I meant p-1/2 not p...

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 12월 12일
if mod(p,1) == 0
  댓글 수: 1
Yuechuan Chen
Yuechuan Chen 2019년 12월 13일
Thank you! Didn't think of this erhh

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2019년 12월 13일
If you want to check whether (p-1)/2 is an integer, then check whether p is an odd number. But since p is a prime number, it seems p is always odd except that p==2.
  댓글 수: 1
Yuechuan Chen
Yuechuan Chen 2019년 12월 13일
Of course! Thank you. Didn't realise.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by