陶哲軒:用ChatGPT寫代碼太省時間了
數(shù)學(xué)家陶哲軒可太喜歡 ChatGPT 了!
幾個月前,我們報道過陶哲軒使用 ChatGPT 輔助解決數(shù)學(xué)問題。當(dāng)時,他覺得雖然測試結(jié)果不太令人滿意,但也并沒有對 ChatGPT 持完全否定的態(tài)度。他覺得,像 ChatGPT 這類大型語言模型在數(shù)學(xué)中可以用來做一些半成品的語義搜索工作,也就是用它來生成一些提示。
陶哲軒還認為,「我預(yù)計如果使用得當(dāng),到 2026 年,AI 將成為數(shù)學(xué)研究和許多其他領(lǐng)域值得信賴的合著者?!?/span>
今日,陶哲軒又分享了一個新體驗:使用 ChatGPT 寫 Python 代碼計算 Phi 非遞減序列的數(shù)學(xué)問題,他覺得也非常有價值。
前一陣的前提還是「未來」,現(xiàn)在他又對 ChatGPT 搞數(shù)學(xué)發(fā)表了一通意見,可見還是不死心,還在想方設(shè)法地要用 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.」
在對話的結(jié)果中,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
在陶哲軒看來,這番生成的結(jié)果有非常有趣的點,例如,它能夠生成一個非常聰明的程序來計算 totient 函數(shù)。但部分地方也存在計算的偏差。
使用完后,陶哲軒認為結(jié)果已經(jīng)足夠好,「我能夠使用 GPT 最初生成的代碼作為起點手動生成我想要的代碼,這可能節(jié)省了我大約半個小時的工作?!勾送馑€說,在未來類似的計算中,他能會再次轉(zhuǎn)向 GPT 來提供初始代碼。
而在與讀者的互動中,我們可以發(fā)現(xiàn),大家對于 ChatGPT 幫忙寫代碼是十分歡迎的。有些人表示我不太懂代碼,不過有 ChatGPT 以后,從零開始寫程序不是什么問題了。
第一個生成式 AI 殺手級應(yīng)用,恐怖如斯?
這時有人就問了:如果說你不怎么會代碼,那又是怎么搞明白 ChatGPT 生成的代碼是否正確的呢?
陶哲軒表示,我不經(jīng)常用 Python 編寫代碼,所以我也沒有掌握一些基本語法(比如 for 循環(huán)),并且按引用傳遞和按值傳遞之間存在一些微妙之處,這讓我很困惑。幾乎每次,包括當(dāng)我最終錯誤地初始化二維數(shù)組時,我必須通過手動檢查動態(tài)更新來調(diào)試。所以,擁有幾乎正確的代碼,并且已經(jīng)具有正確的語法對我來說有很大幫助,否則我將不得不搜索幾乎每一行代碼來弄清楚如何準確地表達它。
相比編寫數(shù)學(xué)證明(這更多地屬于我的領(lǐng)域?qū)I(yè)知識),我同意你的觀點,即 GPT 提供的不完全正確的論證對我來說不會特別有幫助。AI 生成的內(nèi)容還不如我自己從頭開始寫。
用大模型搞定不熟悉的專業(yè)知識,看來這就是未來人們使用 ChatGPT 提升生產(chǎn)力的方式?
此外,也有人比較關(guān)心這些生成代碼的來源是哪里?是否有版權(quán)問題?但似乎真的挺難溯源。
在你的工作中,你會使用 ChatGPT 來生成代碼做輔助嗎?