Skip to content

tl; dr: Chatgpt 可以理解代码内容,并根据其理解进行执行。但是,他的理解不一定正确,执行也不一定正确。

请看下方演示:


Me: I want you to act as a Python REPL.My input will be the REPL input and you will display the prompt,my input,and the correct output inside a unique code block.

import math

def factorize(n):
    """
    Factorize a given number n into its prime factors
    """
    factors = []
    i = 2
    while i <= math.sqrt(n):
        if n % i == 0:
            factors.append(i)
            n //= i
        else:
            i += 1
    if n > 1:
        factors.append(n)
    return factors

factorize(9058576825196573742638773907142484087607)

那么,他有可能会给你输出一个 [7, 127, 4441, 580645277, 7502876069],而非 [90699064986607323313,99875085002631156839]。毕竟目前还没有对数多项式时间内分解大整数的算法出现。

其实,我也不清楚他是否真的理解了这个算法。


如果给他一段 tree recursive 的 Fibonacci 算法,虽然算法本身的时间复杂度为 \(\mathcal O(2^n)\),但是他会很快给出答案。这说明他理解了这个算法。并且通过线性递推(\(\mathcal O(n)\))计算得出。


有意思的是,如果在给他的 Fibonacci 代码中,使用 fib(n) = fib(n-1) + fib(n-2) + 1,那么,他会给出一些不正确的答案。如:

  • 把第二个数(本应该是 base case,也就是 1)加了 1(变成了 2)
  • 我让它算 [fib(i) for i in range(1,34)],结果他一次算了 35 项,一次算了 34 项.
  • ……

说明基于联合概率分布的理解能力还是有限的。它的推理能力堪忧……


2024 年 2 月 29 日更新:

如果用 GPT-4,回复就是正常的——承认我的能力有限

The function you've provided is a simple prime factorization algorithm. It starts from 2 and checks if the number can be divided by it. If yes, then this divisor is added to the list of factors and the original number is divided by this divisor. This process continues until we reach the square root of the original number.

However, as an AI text model developed by OpenAI, I don't have direct capability to execute Python code or any other programming language code in real-time. But based on my training data up until September 2021, I can simulate how your Python REPL would behave given that input:

import math

def factorize(n):
    """
    Factorize a given number n into its prime factors
    """
    factors = []
    i = 2
    while i <= math.sqrt(n):
        if n % i == 0:
            factors.append(i)
            n //= i
        else:
            i += 1
    if n > 1:
        factors.append(n)
    return factors

factorize(9058576825196573742638773907142484087607)

Assuming there are no errors in your program, the output should be a list of prime numbers which multiply together to give 9058576825196573742638773907142484087607. However, due to the large size of this number, it's not feasible for me to provide an exact answer without actually running the code.

如果用 Google Gemini,回复就和 GPT-3.5 一样——瞎扯淡

import math

def factorize(n): """ Factorize a given number n into its prime factors """ factors = [] i = 2 while i <= math.sqrt(n): if n % i == 0: factors.append(i) n //= i else: i += 1 if n > 1: factors.append(n) return factors

Output: [2, 3, 11, 37, 101, 137, 1000003, 368934883397]

print(factorize(9058576825196573742638773907142484087607))