我有下面的java程序,该程序打印所有素数到100,现在我想对其进行修改,以便用户输入该数字,它不会打印范围,但该范围中存在最后一个素数,请告知如何修改以下程序,我也想将返回类型从字符串更改为int
onAttach
现在输出为
class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(primeNumbers);
}
}
我只希望仅打印最后一个素数
Prime numbers from 1 to 100 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
答案 0 :(得分:0)
尝试仅维护状态以获取循环发现的最新素数:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.21'
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/android/android-tools" }
}
dependencies {
classpath 'com.google.gms:google-services:4.1.0'
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/android/android-tools" }
}
}
答案 1 :(得分:0)
您可能想引入一个cv::Vec3f getEyeball(cv::Mat &eye, std::vector<cv::Vec3f> &circles)
{
std::vector<int> sums(circles.size(), 0);
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
for (int i = 0; i < circles.size(); i++)
{
cv::Point center((int)std::round(circles[i][0]),
(int)std::round(circles[i][1]));
int radius = (int)std::round(circles[i][2]);
if ( std::pow(x - center.x, 2)
+ std::pow(y - center.y, 2) < std::pow(radius, 2))
{
sums[i] += value;
}
}
++ptr;
}
}
.
.
.
}
变量(int
),该变量将保存最大质数的值。对问题代码进行了一些小的更改:
int primeNumber
答案 2 :(得分:0)
根据我的理解,您要打印接近100的最大素数。
class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = i+ "";
}
}
System.out.println("Max prime number between 1 to 100 is:");
System.out.println(primeNumbers);
}
}
更新:
可以进一步优化该程序以改善时间复杂度
public class PrimeNumbers{
public static void main (String[] args)
{
int i =0;
int num =0;
int primeNumbers = 0;
for (i = 100; i >= 1; i--)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
primeNumbers = i;
break;
}
}
System.out.println("Max prime number between 1 to 100 is:");
System.out.println(primeNumbers);
}
}
答案 3 :(得分:0)
建议的源代码可以编写得更加结构化:
public class PrimeNumbers {
private static boolean isPrime(int n) {
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(n); i = i + 2)
if (n % i == 0) return false;
return true;
}
public static void main(String[] args) {
int biggestPrimeNumber = 0;
String primeNumbers = "";
for (int n = 2; n <= 100; n++)
if (isPrime(n)) {
biggestPrimeNumber = n;
//Appended the Prime number to the String
primeNumbers += n + " ";
}
System.out.println("Prime numbers from 1 to 100 are: " + primeNumbers);
System.out.println("Last prime number from 1 to 100 is: " + biggestPrimeNumber);
}
}
要检查数字的素数,有很多算法,例如Miller–Rabin primality test,但是一种简单的蛮力方法是检查数字,直到所需数字的sqrt。