鱼C论坛

 找回密码
 立即注册
查看: 148|回复: 1

我的找素数和完全数的代码有没有问题

[复制链接]
发表于 2024-4-6 15:58:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
class thrend1 implements Runnable{
    int sum=0;
    private static boolean isPrime(int k) {
        if(k%2==0&&k>2){
            return false;
        }
        boolean tag=true;
        if(k==2)
            tag=true;
        for(int i=2;i<=Math.sqrt(k);i++){
            if(k%i==0){
                tag=false;
            }
        }
        return tag;
    }
    @Override
    public void run() {
        for (int i = 1; i <10000 ; i++) {
            if(isPrime(i))
                sum+=i;
        }
        System.out.println("素数之和为:"+sum);
    }
}
class thrend2 implements Runnable{
    int sum=0;
   private static boolean isPerfect(int k){
       boolean tag=true;
       int sum1=0;
       for(int i=1;i<k;i++){
           if(k%i==0){
               sum1+=i;
           }
       }
       if(sum1!=k){
           tag=false;
       }
       return tag;
   }
    @Override
    public void run() {
        for (int i = 1; i <10000 ; i++) {
            if(isPerfect(i))
                sum+=i;
        }
        System.out.println("完全数之和:"+sum);
    }
}
public class test02 {
    public static void main(String[] args)throws Exception {
        thrend1 th1=new thrend1();
        thrend2 th2=new thrend2();
        Thread thread1 = new Thread(th1);
        Thread thread2 = new Thread(th2);
        thread1.start();
        thread2.start();
//        thread1.run();
//        thread2.run();
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-6 15:59:08 | 显示全部楼层
您的代码有一些问题。首先,在class名称上有一个小错误,应该是`Thread1`而不是`thrend1`。其次,您在代码中使用了多线程,并且将寻找素数和完全数的逻辑放在了`run`方法中。但是,在您的代码中没有调用`Thread.start()`方法来启动线程,因此代码不会执行。另外,如果您只是想要寻找素数和完全数,并不需要使用多线程,可以直接在主线程中进行操作。

下面是一个修正后的示例代码来寻找素数和完全数。

  1. import java.util.ArrayList;
  2. import java.util.List;

  3. public class PrimeAndPerfect {

  4.     private static boolean isPrime(int num) {
  5.         if (num <= 1) {
  6.             return false;
  7.         }
  8.         
  9.         for (int i = 2; i <= Math.sqrt(num); i++) {
  10.             if (num % i == 0) {
  11.                 return false;
  12.             }
  13.         }
  14.         
  15.         return true;
  16.     }

  17.     private static boolean isPerfect(int num) {
  18.         int sum = 0;
  19.         
  20.         for (int i = 1; i < num; i++) {
  21.             if (num % i == 0) {
  22.                 sum += i;
  23.             }
  24.         }
  25.         
  26.         return sum == num;
  27.     }

  28.     public static void main(String[] args) {
  29.         List<Integer> primes = new ArrayList<>();
  30.         List<Integer> perfects = new ArrayList<>();

  31.         for (int i = 1; i <= 100; i++) {
  32.             if (isPrime(i)) {
  33.                 primes.add(i);
  34.             }
  35.             if (isPerfect(i)) {
  36.                 perfects.add(i);
  37.             }
  38.         }

  39.         System.out.println("素数:");
  40.         for (int prime : primes) {
  41.             System.out.print(prime + " ");
  42.         }

  43.         System.out.println("\n完全数:");
  44.         for (int perfect : perfects) {
  45.             System.out.print(perfect + " ");
  46.         }
  47.     }
  48. }
复制代码


这段代码会输出100以内的素数和完全数。注意,在代码中判断一个数是否为素数时,我们只需要从2到sqrt(n)进行遍历,提高了算法效率。对于完全数的判断,我们遍历小于该数的所有因子并求和,如果和等于该数,则该数为完全数。

希望这个例子对您有所帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 08:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表