用 ChatGPT 写代码太省时间了

几个月前,我们聊过陶哲轩使用 ChatGPT 辅助解决数学问题。当时,他觉得虽然测试结果不太令人满意,但也并没有对 ChatGPT 持完全否定的态度。他觉得,像 ChatGPT 这类大型语言模型在数学中可以用来做一些半成品的语义搜索工作,也就是用它来生成一些提示。

图片

陶哲轩还认为,「我预计如果使用得当,到 2026 年,AI 将成为数学研究和许多其他领域值得信赖的合著者。」

今日,陶哲轩又分享了一个新体验:使用 ChatGPT 写 Python 代码计算 Phi 非递减序列的数学问题,他觉得也非常有价值。

前一阵的前提还是「未来」,现在他又对 ChatGPT 搞数学发表了一通意见,可见还是不死心,还在想方设法地要用 AI 大模型提升自己的工作效率。

图片

在这次体验中,他使用的 Prompt 是「I would like to write a program in some standard programming language (e.g., Python or C++) that, when given a natural number n, calculates the length M (n) of the longest subsequence of {1,…,n} on which the Euler totient function varphi is non-decreasing. For instance, if n = 6, we would have M (n)=5, because varphi is non-decreasing on {1,2,3,4,5} or {1,2,3,4,6} but not {1,2,3,4,5,6}. How would one program this? Ideally I’d like a program that can run reasonably quickly for medium values of n (say as large as 100), suggesting perhaps a dynamic programming method, but initially a brute force approach that only works for small n might be good enough for me.」

图片

在对话的结果中,ChatGPT 先解释了一番,给出了以下 Python 代码:

import math

def phi(n):
    """Compute the Euler totient function for n."""
    result = n
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            while n % i == 0:
                n //= i
            result -= result // i
    if n > 1:
        result -= result // n
    return result

def is_non_decreasing(seq):
    """Check if a sequence is non-decreasing."""
    for i in range(1, len(seq)):
        if seq[i] < seq[i-1]:
            return False
    return True

def longest_non_decreasing_phi_subsequence(n):
    """Find the length of the longest subsequence of {1,...,n} on which phi is non-decreasing."""
    phi_values = [phi(i) for i in range(1, n+1)]
    max_length = 0
    for i in range(n):
        for j in range(i, n):
            if is_non_decreasing(phi_values[i:j+1]):
                max_length = max(max_length, j-i+1)
    return max_length

n = 6
print(longest_non_decreasing_phi_subsequence(n))  # Output: 5

在陶哲轩看来,这番生成的结果有非常有趣的点,例如,它能够生成一个非常聪明的程序来计算 totient 函数。但部分地方也存在计算的偏差。

使用完后,陶哲轩认为结果已经足够好,「我能够使用 GPT 最初生成的代码作为起点手动生成我想要的代码,这可能节省了我大约半个小时的工作。」此外他还说,在未来类似的计算中,他能会再次转向 GPT 来提供初始代码。

扫描二维码关注公众号,回复: 16444880 查看本文章

而在与读者的互动中,我们可以发现,大家对于 ChatGPT 帮忙写代码是十分欢迎的。有些人表示我不太懂代码,不过有 ChatGPT 以后,从零开始写程序不是什么问题了。

图片

第一个生成式 AI 杀手级应用,恐怖如斯?

这时有人就问了:如果说你不怎么会代码,那又是怎么搞明白 ChatGPT 生成的代码是否正确的呢?

陶哲轩表示,我不经常用 Python 编写代码,所以我也没有掌握一些基本语法(比如 for 循环),并且按引用传递和按值传递之间存在一些微妙之处,这让我很困惑。几乎每次,包括当我最终错误地初始化二维数组时,我必须通过手动检查动态更新来调试。所以,拥有几乎正确的代码,并且已经具有正确的语法对我来说有很大帮助,否则我将不得不搜索几乎每一行代码来弄清楚如何准确地表达它。

相比编写数学证明(这更多地属于我的领域专业知识),我同意你的观点,即 GPT 提供的不完全正确的论证对我来说不会特别有帮助。AI 生成的内容还不如我自己从头开始写。

图片

用大模型搞定不熟悉的专业知识,看来这就是未来人们使用 ChatGPT 提升生产力的方式?

此外,也有人比较关心这些生成代码的来源是哪里?是否有版权问题?但似乎真的挺难溯源。

图片

在你的工作中,你会使用 ChatGPT 来生成代码做辅助吗?

猜你喜欢

转载自blog.csdn.net/qq_34160248/article/details/132652544