打印出从阵列素数 [英] printing out prime numbers from array

查看:147
本文介绍了打印出从阵列素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从方法的阵列打印出所有素数。我可以用一个INT做
但不知道如何从数组中返回某些数字。感谢您的帮助!

 公共静态布尔isPrime(INT []选项卡){
        布尔素= TRUE;
        的for(int i = 3; I< =的Math.sqrt(标签[I]); I + = 2)
            如果(标签[I]%我== 0){
                素= FALSE;
                打破;
            }
        的for(int i = 0; I< tab.length;我++)
        如果((标签[I]%2 = 0&放大器;!&放大器;素&放大器;&放大器;标签[I]→2)||标签[I] == 2){
            返回true;
                }其他{
            返回false;
        }
        //返回素;}

感谢你们俩。看起来似乎解决的:

 公共静态无效isPrime(INT []选项卡){
        的for(int i = 0; I< tab.length;我++){
            如果(isPrimeNum(标签由[i])){
                的System.out.println(标签[I]);
            }
        }
    }    公共静态布尔isPrimeNum(INT N){
        布尔素= TRUE;
        为(长我= 3; I< =的Math.sqrt(N); I + = 2){
            如果(N%我== 0){
                素= FALSE;
                打破;
            }
        }
        如果((N%2 = 0&放大器;!&放大器;素&放大器;&放大器; N大于2)|| n ==可2){
            返回true;        }其他{
            返回false;
        }
    }


解决方案

我建议你分开这到两种方法:


  • 一个方法来确定一个数字是否为素数

  • 通过阵列来迭代的一种方法,调用与每个号码的第一个方法,并打印出该方法返回true的值。

这分离出两个问题整齐。如果你被困在究竟是如何做到这一点,请给其位,你觉得很难的细节。 (我假设这是功课,这就是为什么我不只是包括code)

I'd like to print out all prime numbers from an array with method. I can do it with one int but don't know how to return certain numbers from array. Thanks for help!

public static boolean isPrime(int [] tab) {
        boolean prime = true;
        for (int i = 3; i <= Math.sqrt(tab[i]); i += 2)
            if (tab[i] % i == 0) {
                prime = false;
                break;
            }
        for(int i=0; i<tab.length; i++)
        if (( tab[i]%2 !=0 && prime && tab[i] > 2) || tab[i] == 2) {
            return true;
                } else {
            return false;
        }
        //return prime;

}

thanks both of you. Seems like its solved:

public static void isPrime(int[] tab) {
        for (int i = 0; i < tab.length; i++) {
            if (isPrimeNum(tab[i])) {
                System.out.println(tab[i]);
            }
        }


    }

    public static boolean isPrimeNum(int n) {
        boolean prime = true;
        for (long i = 3; i <= Math.sqrt(n); i += 2) {
            if (n % i == 0) {
                prime = false;
                break;
            }
        }
        if ((n % 2 != 0 && prime && n > 2) || n == 2) {
            return true;

        } else {
            return false;
        }
    }

解决方案

I would suggest you separate this into two methods:

  • One method to determine whether a single number is prime
  • One method to iterate through an array, call the first method with each number, and print out the values for which the method returns true.

That separates out the two concerns neatly. If you're stuck on exactly how to do this, please give details of which bit you find hard. (I'm assuming this is homework, which is why I haven't just included the code.)

这篇关于打印出从阵列素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆