试验GPT写代码两例

文章提供了两个Java代码示例,一个是简单的HelloWorld程序,另一个是验证100内哥德巴赫猜想的程序,该程序利用布尔数组判断质数,并检查所有偶数是否能表示为两个质数之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

试验GPT写代码两例

写一个HeLLo WOrLd的Java代码

java复制代码

public class HelloWorld { 
    public static void main(String[] args) { 
        System.out.println("Hello World!"); 
    } 
} 

写一段验证100内哥德巴赫猜想的Java代码
执行结果如下所示:

4 = 2 + 2
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17
22 = 3 + 19
24 = 5 + 19
26 = 3 + 23
28 = 5 + 23
30 = 7 + 23
32 = 3 + 29
34 = 3 + 31
36 = 5 + 31
38 = 7 + 31
40 = 3 + 37
42 = 5 + 37
44 = 3 + 41
46 = 3 + 43
48 = 5 + 43
50 = 3 + 47
52 = 5 + 47
54 = 7 + 47
56 = 3 + 53
58 = 5 + 53
60 = 7 + 53
62 = 3 + 59
64 = 3 + 61
66 = 5 + 61
68 = 7 + 61
70 = 3 + 67
72 = 5 + 67
74 = 3 + 71
76 = 3 + 73
78 = 5 + 73
80 = 7 + 73
82 = 3 + 79
84 = 5 + 79
86 = 3 + 83
88 = 5 + 83
90 = 7 + 83
92 = 3 + 89
94 = 5 + 89
96 = 7 + 89
98 = 19 + 79
100 = 3 + 97

进程已结束,退出代码0

下面是一段Java代码,用于验证100内的哥德巴赫猜想,即对于每个大于2的偶数,都可以写成两个质数之和。

public class GoldbachConjecture { 
    public static void main(String[] args) { 
        int limit = 100; 
        boolean[] isPrime = new boolean[limit + 1]; 
        // 初始化isPrime数组 
        for (int i = 2; i <= limit; i++) { 
            isPrime[i] = true; 
        } 
        // 找出所有质数 
        for (int i = 2; i * i <= limit; i++) { 
           if (isPrime[i]) { 
               for (int j = i * i; j <= limit; j += i)   {
                   isPrime[j] = false; 
               }
           } 
       } 
       // 检验哥德巴赫猜想 
       for (int i = 4; i <= limit; i += 2) {
          boolean found = false; 
          for (int j = 2; j <= i / 2; j++) { 
              if (isPrime[j] && isPrime[i - j]) { 
                  found = true; System.out.printf("%d = %d + %d\n", i, j, i - j); 
                  break; 
               } 
          } 
          if (!found) { System.out.println("Goldbach Conjecture is false."); }
       } 
  }
} 

这段代码利用了一个布尔数组isPrime来记录每个数字是否为质数,然后使用嵌套的循环遍历所有可能的相加组合,找出两个质数加起来等于目标数字。如果没有找到合适的组合,则说明哥德巴赫猜想不成立。

—the—end—

### 使用GPT代码时添加自定义指令的方法 当利用基于GPT的模型来辅助编程工作时,可以通过精心设计提示词(prompt)的方式加入自定义指令。这不仅能够指导AI更精确地理解需求,还能提高生成代码的质量和准确性。 #### 设计有效的提示词结构 为了使GPT更好地遵循特定指示,在构建提示词时应考虑以下几个方面: - **清晰的任务描述**:明确告知要实现的功能或解决的问题是什么。 ```plaintext Task: Create a Python function named 'calculate_area' which calculates the area of different shapes based on user input. ``` - **输入输出格式说明**:指定函数接收什么样的参数以及返回的结果形式。 ```plaintext Input parameters should be shape type (string), length (float/int), width (float/int). The output must be an integer or float representing the calculated area value. ``` - **附加约束条件**:如果存在任何特殊要求,比如性能优化目标、兼容性考量或是编码风格偏好等,则需一并指出。 ```plaintext Ensure all variables are properly documented using docstrings following PEP 257 conventions. Additionally, include error handling for invalid inputs such as negative numbers or non-existent shape types. ``` 通过这种方式组合而成的复杂提示可以有效地引导像GPT这样的大型语言模型按照预期方式执行任务[^1]。 #### 实际应用案例展示 下面是一个具体的例子,展示了如何向GPT提供带有自定义指令的提示以获取所需功能的Python代码片段: ```python # Prompt given to GPT model: Task Description: Write a simple web server in Flask framework that serves static HTML pages located under './static/' directory. Input/Output Specifications: No direct API endpoints required; only serve files from specified folder path upon HTTP GET requests made by clients visiting http://localhost:<port>/path/to/file.html where <port> is configurable via environment variable PORT. Additional Requirements: Use logging module to record access logs at INFO level including timestamp, client IP address, requested URL, status code returned, and response time measured in milliseconds. Apply best practices regarding security headers configuration according to OWASP recommendations. ``` 上述示例中的提示包含了详细的上下文信息,使得GPT能够在充分理解开发者意图的基础上生成满足业务逻辑和技术标准双重要求的应用程序源码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值