この記事は主にPythonプログラミング正の整数が素数であるかどうかを判断する方法を紹介しており、Pythonの数学的演算関連の操作スキルを必要とする友人はそれを参照できます
この記事ではPythonの例を説明します。正の整数
かどうかを判断するプログラミング 整数が素数かどうかを判断するメソッド。参考のために皆さんと共有してください。詳細は次のとおりです:import string import math #判断是否素数的函数 def isPrime(n): if(n<2): return False; elif(n==2): return True; elif(n>2): for d in range(2,int(math.ceil(math.sqrt(n))+1)): if(n%d==0): return False; return True; num=input(); strNum=list(str(num)) #将输入值转换为List字符串 flag=True; #设置一个标志位 #以下循环用于对用户输入数值进行循环位移后得到的所有结果 for i in range(0,len(strNum)): lastP=strNum.pop();#获取并删除最后一位 strNum.insert(0,lastP);#将上一步删除的数字添加到最前面 stempNumStr=''; #用于保存某一步位移结果的临时变量 for each in strNum: stempNumStr+=each;#将位移后的字符串合并 stempNum=string.atoi(stempNumStr);#转换为整形 #或者 stempNum= ( num//(10**i) ) + (num%(10**i))*(10**(lens-i)) if(isPrime(stempNum)==False):#再判断位移后的这个数是否是素数,如果不是 flag=False;#则直接判定当前用户输入的数字不是循环素数 print('%s not Loop prime number'%num); break;#并中止后续计算,退出循环 if(flag==True):#如果Flag的值依然为True,说明这个数一定是循环素数 print('%s is Loop prime number'%num);実行結果は次のとおりです:
以上が正の整数が素数かどうかを判断する Python プログラミングのサンプル コードの共有の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

SublimeText3 中国語版
中国語版、とても使いやすい

WebStorm Mac版
便利なJavaScript開発ツール

Dreamweaver Mac版
ビジュアル Web 開発ツール
