1. ä¸ä¾èµäºä»»ä½æµ®å¨çé®é¢ï¼math.sqrt(x)æx**0.5ï¼æ¯ä½ ä¸è½çæ£ç¡®å®å®çåç¡®ï¼å¯¹å
å大çæ´æ°xï¼å®ä¸ä¼æ¯ï¼çè³æå¯è½æº¢åºï¼ã幸è¿çï¼å¦ææ¯ä¸æ¥äº;-)æå¾å¤çº¯æ´æ°çæ¹æ³ï¼å¦ä¸é¢ç...ï¼
def is_square(apositiveint):
x = apositiveint // 2
seen = set([x])
while x * x != apositiveint:
x = (x + (apositiveint // x)) // 2
if x in seen: return False
seen.add(x)
return True
for i in range(110, 130):
print i, is_square(i)
æ示ï¼å®æ¯åºäºâ
å·´æ¯ä¼¦ç®æ³âç
å¹³æ¹æ ¹ï¼è¯·åé
ç»´åºç¾ç§ãå®éç¨äºä»»ä½æ£æ°ï¼èæ¨æç»§ç» ç¼è¾ï¼è®©æ们çä¸ä¸ªä¾å...
x = 12345678987654321234567 ** 2
for i in range(x, x+2):
print i, is_square(i)
è¿ç§çç»ï¼æ ¹æ®éè¦ï¼å太;-)ä¸ä¸ªåççéé¢ï¼
152415789666209426002111556165263283035677489 True
152415789666209426002111556165263283035677490 False
请æ¨æåºäºä¸ç§åºäºæµ®ç¹ç»æç解å³æ¹æ¡ä¹å CodeGo.netï¼ç¡®ä¿ä»ä»¬æ£ç¡®å°å·¥ä½å¨è¿ä¸ªç®åçä¾å-å®ä¸æ¯é£ä¹é¾ï¼ä½ åªéè¦ä¸äºé¢å¤çæ£æ¥ï¼ä»¥é²æ¯æç¹è¿ï¼ï¼åªæ¯éè¦å¤ä¸ç¹çå
³æã ç¶åå°è¯ç¨x**7并æ¾å°è§£å³æ¨ä¼å¾å°è¿ä¸ªé®é¢å·§å¦çæ¹å¼ï¼
OverflowError: long int too large to convert to float
ä½ å¿
é¡»å¾å°è¶æ¥è¶å¤çèªæçæ°éä¸æå¢å ï¼å½ç¶ã å¦ææå¾çæ¥ï¼å½ç¶ï¼ægmpy-ä½åæ¥ï¼æææ¾åå;-)ã
>>> import gmpy
>>> gmpy.is_square(x**7)
1
>>> gmpy.is_square(x**7 + 1)
0
æ¯åï¼æç¥éï¼è¿åªæ¯å¾å®¹ææè§åä½å¼ï¼æç¹ææ»ä½æè§å¯¹Pythonç;-)çæ¹å¼-没æèªæå¯è¨ï¼åªæ¯å®ç¾çç´æ¥åç®åï¼åï¼å¨gmpyï¼ç»å¯¹é度çæ
åµä¸;-) ...
2. ç¨
çé¡¿çå¿«éé¶ææ¥è¿çæ´æ°çå¹³æ¹æ ¹ï¼é£ä¹å®å¹³æ¹ï¼ççå®æ¯å¦æ¯ä½ çå·ç ãè§isqrtã
3. å ä¸ºä½ æ°¸è¿æ æ³é å½æµ®å¨ï¼å¦è®¡ç®å¹³æ¹æ ¹çè¿äºæ¹å¼ï¼ï¼ä¸ä¸ªä¸æåºéå°æ¯å¯¹å¤ç
import math
def is_square(integer):
root = math.sqrt(integer)
if int(root + 0.5) ** 2 == integer:
return True
else:
return False
æ³åintegeræ¯9ãmath.sqrt(9)å¯è½æ¯3.0çï¼ä½å®ä¹å¯ä»¥æ¯å2.99999æ3.00001ï¼å æ¤ç°è¾ç»æ马ä¸æ¯ä¸å¯é çãç¥éint
åæ´æ°å¼ï¼éè¿å¢å æµ®ç¹å¼0.5æ们ä¼å¾å°æ们è¦æ¾çï¼å¦ææ们æ¯å¨ä¸ä¸ªèå´å
çå¼ï¼å
¶ä¸floatä»ç¶æ足å¤ç»çå辨çæ¥è¡¨ç¤ºéè¿çä¸ä¸ªä¸ºæ们ææå¾
çæ°åã
4. ææ¯æ°æ¥çå æ 溢åºï¼å¹¶åäºä¸ä¸ªå¿«éè±èæ¾å°è§£å³çåæ³ãæåªæ¯å¼ è´´å¨å¦ä¸ä¸ªçº¿ç¨ï¼å¯»æ¾å®ç¾çæ£æ¹å½¢ï¼ä¸çä¾åï¼ä¸ä¸ªç»å¾®çååä¸é¢ï¼ææ³æä¼å
æ¬ä»ä¹ï¼æè´´å¨è¿éæä¸ä¸ªç»å¾®çååï¼ä½¿ç¨nsqrtä½ä¸ºä¸ä¸ªä¸´æ¶åéï¼ï¼å¦æå®çå©ç/使ç¨ï¼
import math
def is_perfect_square(n):
if not ( ( isinstance(n, int) or isinstance(n, long) ) and ( n >= 0 ) ):
return False
else:
nsqrt = math.sqrt(n)
return nsqrt == math.trunc(nsqrt)
5. ä½ å¯ä»¥
äºè¿å¶æç´¢çå形平æ¹æ ¹ãå¹³æ¹çç»æï¼ä»¥ç¡®å®å®çåå§å¼ç¸å¹é
ã ä½ å¯è½ä¼æ´å¥½è¿ä¸FogleBirdsåç-è½ç¶å°å¿ï¼å 为
æµ®ç¹æ°æ¯è¿ä¼¼çï¼å®å¯ä»¥æåºè¿ç§æ¹æ³äºãä½ å¯ä»¥å¨ååä¸å¾å°ä¸ä¸ªåé³æ§ä»ä¸ä¸ªå¤§çæ´æ°ï¼è¾å®ç¾çæ£æ¹å½¢ï¼ä¾å¦ï¼ç±äºä¸¢å¤±ç²¾åº¦1以ä¸ã
6.
>>> def f(x):
... x = x ** 0.5
... return int(x) == x
...
>>> for i in range(10):
... print i, f(i)
...
0 True
1 True
2 False
3 False
4 True
5 False
6 False
7 False
8 False
9 True
7. å³å®å¤ä¹
çæ°éå°±è¶å¤§ã éåå¢é0.000000000000 ....... 000001 è§ï¼å¦æï¼SQRTï¼Xï¼ï¼^ 2-xæ¯å¤§äº/çäº/大äºÎ´è¾å°å¹¶ä¸åºäºå¢é误差å³å®ã
8. æä¸ç¥éPythonçï¼ä½ä½ å¯ä»¥ä¸å欢ï¼
function isSquare(x) = x == floor(sqrt(x) + 0.5)^2
ä¹å°±æ¯è¯´ï¼æ¿ä¸ä¸ªæ°ï¼æ±å¹³æ¹æ ¹ï¼åèäºå
¥å°ææ¥è¿çæ´æ°ï¼å®å¹³æ¹ï¼å¹¶æµè¯å®æ¯ä½ä¸ºåæ¥çå·ç ã ï¼floor并å å
¥0.5åæ¯ä¸ºäºé²æ¢ç±»ä¼¼æ¡ä»¶sqrt(4)åå½1.9999999...ç±äº
æµ®ç¹è¿ç®ï¼éº¦å
grahamsæåºãï¼ å¦æä½ æå
´è¶£ï¼æ¾ç»æä¸ä¸ªå¾å¥½çå¤æ以æå¿«çæ¹å¼ï¼å¦æä¸ä¸ªæ´æ°çå¹³æ¹æ ¹æ¯ä¸ä¸ªæ´æ°ã ç¼è¾æ¾æ¸
ã
9. 该åå¤ä¸å±äºä½ çdeclarativeçé®é¢ï¼èæ¯ä¸ä¸ªéå«çé®é¢ï¼æå¨æ¨åå¸ç代ç ä¸çå°ï¼å³âå¦ä½æ£æ¥æ¯å¦æ¯æ´æ°ï¼â ä¼å
个åçä½ é常å¾å°è¿ä¸ªé®é¢æ¯âä¸è¦ï¼â并ä¸è¿æ¯ççï¼å¨Pythonï¼ç±»åæ£æ¥ä¸åºè¯¥åçäºæ
ã 对äºé£äºæå°æ°çå¼å¸¸ï¼ä¸è¿ï¼ä¸æ¯å¯»æ¾æ°åç
å符串表示å°æ°ç¹ï¼é£ä¸è¥¿åisinstanceå½æ°ï¼
>>> isinstance(5,int)
True
>>> isinstance(5.0,int)
False
å½ç¶éç¨äºåéï¼èä¸æ¯ä¸ä¸ªå¼ãå¦æææ³ç¡®å®è¯¥å¼æ¯å¦æ¯ä¸ä¸ªæ´æ°ï¼æä¼åå°è¿ä¸ç¹ï¼
>>> x=5.0
>>> round(x) == x
True
ä½æ£å¦å
¶ä»äººå·²ç»è¯¦ç»ä»ç»ï¼ä¹æè¿ç§äºæ
ç大å¤æ°éç©å
·çä¾åæ¥å 以èèæµ®ç¹é®é¢ã
10. ææ轻微çåå§å·´æ¯ä¼¦çæ¹æ³ãåè代ä¹çæ¯ä¸å¥ä»¥åå¨æ¯ä¸ªçæçè¿ä¼¼ï¼åªæ¯æè¿ç两个è¿ä¼¼çåå¨åæ ¸å¯¹çµæµè¿ä¼¼ãè¿ä¿åäºå¤§éçéè¿æ´å¥çè¿ä¼¼å¼ç浪费æ£æ¥ãæçjavaï¼èä¸æ¯pythonåBigIntegerç±»ï¼èä¸æ¯ä¸ä¸ªæ£å¸¸çåå§æ´æ°ã
BigInteger S = BigInteger.ZERO;
BigInteger x = BigInteger.ZERO;
BigInteger prev1 = BigInteger.ZERO;
BigInteger prev2 = BigInteger.ZERO;
Boolean isInt = null;
x = S.divide(BigInteger.valueOf(2));
while (true) {
x = x.add(preA.divide(x)).divide(BigInteger.valueOf(2));
if (x.pow(2).equals(S)) {
isInt = true;
break;
}
if (prev1.equals(x) || prev2.equals(x)) {
isInt = false;
break;
}
prev2 = prev1;
prev1 = x;
}