LoginSignup
0
0

More than 5 years have passed since last update.

ゴルドバッハの定理をjavaで書いてみた

Posted at

ゴルドバッハの定理・・・・全ての偶数は素数の和で表すことができる。
この定理をjavaで書いてみる。

public class Qiita {
public static void main(String[] args) {
    boolean bl=true;
    int num=0;
    int n=6;
    String ans="";
    int[] sosuu=new int[24];

    for(int i=3; i<=100; i++) {
        for(int j=2; j<=(i/2); j++) {
            if(i%j==0){
                bl=false;
            }
        }
        if(bl==true){
            sosuu[num]=i;
            num++;
        }else{
            bl=true;
        }
    }
    while(n<=100) {
        for(int i=0; i<24; i++) {
            for(int j=0; j<24; j++) {
                if(n==(sosuu[i]+sosuu[j]) &&i<=j) {
        ans+="["+sosuu[i]+","+sosuu[j]+"]";
                }
            }
        }
        System.out.print(n+"\t"+":"+ans);
        ans="";
        System.out.println();
        n+=2;
    }
}
}

forを使いすぎてプログラムが汚くなってしまいましたが、何とか完成させることができました。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0