Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    資源
    概覽詞彙表系統提示詞
    提示詞庫Cosmic KeystrokesCorporate clairvoyantWebsite wizardExcel formula expertGoogle apps scripterPython bug busterTime travel consultantStorytelling sidekick引用您的來源SQL sorcererDream interpreterPun-ditCulinary creatorPortmanteau poetHal the humorous helperLaTeX legendMood colorizerGit gudSimile savantEthical dilemma navigatorMeeting scribeIdiom illuminatorCode consultantFunction fabricatorNeologism creatorCSV converterEmoji encoderProse polisherPerspectives pondererTrivia generatorMindfulness mentorSecond-grade simplifierVR fitness innovatorPII purifierMemo maestroCareer coachGrading guruTongue twisterInterview question crafterGrammar genieRiddle me thisCode clarifierAlien anthropologistData organizerBrand builderEfficiency estimatorReview classifierDirection decoderMotivational museEmail extractorMaster moderatorLesson plannerSocratic sageAlliteration alchemistFuturistic fashion advisorPolyglot superpowersProduct naming proPhilosophical musingsSpreadsheet sorcererSci-fi scenario simulatorAdaptive editorBabel's broadcastsTweet tone detectorAirport code analyst
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    提示詞庫

    效率估算器

    計算函數和演算法的時間複雜度。

    將此提示複製到我們的開發者 Console 中親自試試看!

    內容
    System您的任務是分析提供的函數或演算法,並使用大O記號計算其時間複雜度。逐步解釋您的推理過程,描述您如何得出最終的時間複雜度。在確定時間複雜度時考慮最壞情況。如果函數或演算法包含多個步驟或嵌套迴圈,請提供每個步驟的時間複雜度,然後給出整個函數或演算法的總體時間複雜度。假設使用的任何內建函數或操作的時間複雜度為O(1),除非另有說明。
    Userdef example_function(n): for i in range(n): print(i) for j in range(n): for k in range(n): print(j, k)

    範例輸出

    為了分析給定函數 example_function(n) 的時間複雜度,我們將其分解為步驟並考慮最壞情況。

    步驟1:分析第一個迴圈

    for i in range(n):
       print(i)
    

    這個迴圈迭代 n 次,並在每次迭代中執行一個常數時間操作(print(i))。因此,此步驟的時間複雜度為O(n)。

    步驟2:分析嵌套迴圈

    for j in range(n):
        for k in range(n):
            print(j, k)
    

    外層迴圈迭代 n 次,對於每次迭代,內層迴圈也迭代 n 次。在內層迴圈的每次迭代中,執行一個常數時間操作(print(j, k))。因此,嵌套迴圈的時間複雜度為O(n * n) = O(n^2)。

    步驟3:結合時間複雜度
    函數 example_function(n) 由兩個步驟組成:第一個迴圈和嵌套迴圈。總體時間複雜度是每個步驟時間複雜度的總和。

    第一個迴圈的時間複雜度:O(n)
    嵌套迴圈的時間複雜度:O(n^2)

    為了確定總體時間複雜度,我們考慮主導項,即增長階數最高的項。在這種情況下,O(n^2) 主導 O(n)。

    因此,函數 example_function(n) 的總體時間複雜度為O(n^2)。

    在最壞情況下,當 n 很大時,嵌套迴圈將主導執行時間,函數將具有二次時間複雜度。


    API 請求

    import anthropic
    
    client = anthropic.Anthropic(
        # defaults to os.environ.get("ANTHROPIC_API_KEY")
        api_key="my_api_key",
    )
    message = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1000,
        temperature=0,
        system="您的任務是分析提供的函數或演算法,並使用大O記號計算其時間複雜度。逐步解釋您的推理過程,描述您如何得出最終的時間複雜度。在確定時間複雜度時考慮最壞情況。如果函數或演算法包含多個步驟或嵌套迴圈,請提供每個步驟的時間複雜度,然後給出整個函數或演算法的總體時間複雜度。假設使用的任何內建函數或操作的時間複雜度為O(1),除非另有說明。",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "def example_function(n):\n    for i in range(n):\n        print(i)\n\n    for j in range(n):\n        for k in range(n):\n            print(j, k)"
                    }
                ]
            }
        ]
    )
    print(message.content)
    
    • API 請求