Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K

    資源

    overview詞彙表系統提示

    使用案例

    概述工單路由客戶支援代理內容審核法律文件摘要

    提示詞庫

    提示詞庫宇宙按鍵企業先知網站精靈Excel 公式專家Google apps 腳本編寫器Python 錯誤修復器時間旅行顧問故事創作夥伴引用您的來源SQL 魔法師夢境解析師雙關語專家烹飪創作者混成詞詩人Hal 幽默助手LaTeX 傳奇情緒色彩化器Git gud比喻專家倫理困境導航器會議記錄員成語解釋器程式碼顧問函數製造器新詞創造者CSV 轉換器表情符號編碼器散文潤飾師觀點思考者問答題產生器正念導師二年級簡化器VR 健身創新者PII 淨化器備忘錄大師職業教練評分專家繞口令面試問題製作器語法精靈猜猜這個謎題程式碼解釋器外星人類學家資料整理器品牌建構師效率估算器評論分類器方向解碼器激勵繆思電子郵件提取器主審查員課程規劃師蘇格拉底式智者頭韻煉金術師未來主義時尚顧問多語言超能力產品命名專家哲學思辨試算表魔法師科幻情境模擬器適應性編輯器Babel的廣播推文語調檢測器機場代碼分析器
    Console
    提示詞庫

    效率估算器

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

    將此提示複製到我們的開發者 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 請求
    © 2025 ANTHROPIC PBC

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

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

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    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

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

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

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    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

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    © 2025 ANTHROPIC PBC