英文:
How can I print the prime numbers ending with 1 in a given range and the nearest one after b?
问题
import java.util.*;
public class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int i, j, count;
for (i = a; i <= b; i++) {
for (j = 2; j <= b; j++) {
if (i % j == 0)
break;
}
if (j == i && j % 10 == 1) {
System.out.println(i);
}
}
}
}
请注意,我已经按照你的要求将代码部分保持原样,没有进行翻译。如果你有关于代码的问题或需要进一步的帮助,请随时提问。
英文:
-
Sample input :
10
100 -
Sample output :
11,31,41,61,71,101 -
from the above code I can get the sample output value upto the value 71,how can I get nearest prime number ending with 1 after b.
Here is the code I tried:
import java.util.*;
public class Program{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
int i,j,count;
for(i=a;i<=b;i++)
{
for(j=2;j<=b;j++)
{
if(i%j==0)
break;
}
if(j==i && j%10==1)
{
System.out.println(i);
}
}
}
答案1
得分: 0
这里是您可以操作的方法,无限运行第一个循环,并在 i 大于 b 且以 1 结尾的素数时中断它。
这是对您的代码稍作修改的实现示例:
import java.util.*;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
boolean flag = false;
int i, j, count;
for (i = a;; i++) {
if (i > b) flag = true;
for (j = 2; j <= i; j++) {
if (i % j == 0)
break;
}
if (j == i && j % 10 == 1) {
System.out.println(i);
if (flag) break;
}
}
}
}
英文:
Here's how you can do it, run the first loop indefinitely and break it when i is greater than b and it's a prime with 1 at the end.
This is the implementation just modifying your code a bit-
import java.util.*;
class Program{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
boolean flag = false;
int i,j,count;
for(i=a;;i++) {
if(i>b) flag = true;
for(j=2;j<=i;j++) {
if(i%j==0)
break;
}
if(j==i && j%10==1) {
System.out.println(i);
if(flag) break;
}
}
}
}
答案2
得分: 0
在两个for循环中都添加条件,类似于I <= b*2。
之后在打印这个序列之前,再添加一个if块,或者将条件添加到if(j==1 && j%10==1)中。
条件是:
你需要检查i的值以及它是否大于b。如果它大于b,那么它应该以1结尾并且是最接近的值,否则不需要打印出来,而且可以中断循环,因为其他值都不需要。
英文:
Add condition in both the for loops like I<= b*2;
After that before printing the series add one more if block or add the condition in if(j==1 && j%10==1)
Condition-
You should have to check what is value of i and whether it's greater than b or not. If it's greater than b then it should be end with 1 and nearer value otherwise no need to print and break it as other values you don't need at all.
答案3
得分: 0
尝试使用一些改进。
- 首先检查是否可以被2整除。
- 然后,既然2已经处理过了,只需被奇数整除<br>
从3开始,不超过待测试数的平方根。 - 然后继续,直到满足搜索参数。
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
outer:
for (int i = a; i <= Integer.MAX_VALUE; i++) {
// 首先检查是否可以被2整除,以便以后可以以2递增
if (i % 2 == 0) {
continue;
}
// 从3开始,以2为增量,增加到i的平方根
for (int j = 3; j <= Math.sqrt(i); j+=2) {
if (i % j == 0) {
// 不是素数,因此继续外部循环
continue outer;
}
}
if (i % 10 == 1) {
System.out.print(i + " ");
// 继续搜索直到i > b。
if (i > b) {
break;
}
}
}
对于输入10 100
,输出为
11 31 41 61 71 101
英文:
Try this with a few improvements.
- first check to see if divisible by 2
- then, since 2 is taken care of, divide only by odd numbers<br>
starting with 3, not exceeding the sqrt of the number under test. - then just continue until the search parameter are met.
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
outer:
for (int i = a; i <= Integer.MAX_VALUE; i++) {
// first check division by 2 so you can increment by 2 later
if (i % 2 == 0) {
continue;
}
// increment from 3 to square root of i by 2's
for (int j = 3; j <= Math.sqrt(i); j+=2) {
if (i % j == 0) {
// not prime so continue with outer loop
continue outer;
}
}
if (i % 10 == 1) {
System.out.print(i + " ");
// keep searching until i > b.
if (i > b) {
break;
}
}
}
for input of 10 100
Prints
11 31 41 61 71 101
</details>
# 答案4
**得分**: 0
你无需将一个数字除以它前面的所有数字来判断它是否为质数。只需检查它的平方根以下的数字即可。请参阅https://en.wikipedia.org/wiki/Primality_test。操作如下:
使用 `for` 循环
===
```java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入两个以空格分隔的整数:");
int a = in.nextInt();
int b = in.nextInt();
int i, j, sqrt;
// 注意,在声明 'for' 循环时有三个部分:
// for (初始化; 条件; 改变),其中没有哪个部分是必需的。在下面给出的循环语法中没有放置终止条件。终止条件已在打印质数之后设置。
for (i = a;; i++) {
sqrt = (int) Math.sqrt(i);
for (j = 2; j <= sqrt; j++) {
if (i % j == 0) {
break;
}
}
// 如果 j 的循环没有中断,该数字是质数。注意 1 不是质数。此外,数字的最后一位应为 1。
if (j > sqrt && Math.abs(i) != 1 && i % 10 == 1) {
System.out.print(i + " "); // 打印质数
if (i >= b) { // 终止条件
break;
}
}
}
}
}
或者,使用 while
循环
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入两个以空格分隔的整数:");
int a = in.nextInt();
int b = in.nextInt();
int i = a, j, sqrt;
while (true) {
sqrt = (int) Math.sqrt(i);
for (j = 2; j <= sqrt; j++) {
if (i % j == 0) {
break;
}
}
// 如果 j 的循环没有中断,该数字是质数。注意 1 不是质数。此外,数字的最后一位应为 1。
if (j > sqrt && Math.abs(i) != 1 && i % 10 == 1) {
System.out.print(i + " "); // 打印质数
if (i >= b) { // 终止条件
break;
}
}
i++;
}
}
}
一个示例运行:
输入两个以空格分隔的整数:10 100
11 31 41 61 71 101
另一个示例运行:
输入两个以空格分隔的整数:10 200
11 31 41 61 71 101 131 151 181 191 211
英文:
You do not need to divide a number by numbers up to it in order to check if it is prime. You just need to check up to its square root. Check https://en.wikipedia.org/wiki/Primality_test. Do it as follows:
Using a for
loop
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter two integers separated by space: ");
int a = in.nextInt();
int b = in.nextInt();
int i, j, sqrt;
// Note that there are three sections in the declaration of a 'for' loop:
// for(initialization;condition;change) where none of the sections is
// mandatory. There is no condition put in the loop syntax given below. The
// condition for termination has been put after printing the prime number.
for (i = a;; i++) {
sqrt = (int) Math.sqrt(i);
for (j = 2; j <= sqrt; j++) {
if (i % j == 0) {
break;
}
}
// If the loop with j completed without a break, the number is prime. Note that
// 1 is not a prime number.Also, the last digit of the number should be 1.
if (j > sqrt && Math.abs(i) != 1 && i % 10 == 1) {
System.out.print(i + " "); // Print the prime
if (i >= b) {// Condition for termination
break;
}
}
}
}
}
Alternatively, using a while
loop
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter two integers separated by space: ");
int a = in.nextInt();
int b = in.nextInt();
int i = a, j, sqrt;
while (true) {
sqrt = (int) Math.sqrt(i);
for (j = 2; j <= sqrt; j++) {
if (i % j == 0) {
break;
}
}
// If the loop with j completed without a break, the number is prime. Note that
// 1 is not a prime number.Also, the last digit of the number should be 1.
if (j > sqrt && Math.abs(i) != 1 && i % 10 == 1) {
System.out.print(i + " "); // Print the prime
if (i >= b) {// Condition for termination
break;
}
}
i++;
}
}
}
A sample run:
Enter two integers separated by space: 10 100
11 31 41 61 71 101
Another sample run:
Enter two integers separated by space: 10 200
11 31 41 61 71 101 131 151 181 191 211
答案5
得分: 0
我假设您需要满足以下条件的质数:
- 在一个范围内(从较小到较大)
- 以1结尾
- 最接近查询值
如果是这样的话,最好的策略是在两个方向(低于和高于)进行外向搜索,并在首次找到以1结尾的质数时停止。
public static int nearest1Prime(final int lower, final int upper, final int val)
{
if(val < lower || val > upper) return 0;
int before, after;
if((val % 10) == 1)
{
if(isPrime(val))
return val;
before = val - 10;
after = val + 10;
}
else
{
int base = 10 * (val / 10);
if(val == base)
{
after = base+1;
before = after-10;
}
else
{
before = base+1;
after = before+10;
}
}
int prime = 0;
while(prime == 0 && (before >= lower || after <= upper))
{
if(before >= lower && isPrime(before))
prime = before;
if(after <= upper && isPrime(after) && (prime == 0 || (after-val) < (val-before)))
prime = after;
before -= 10;
after -= 10;
}
return prime;
}
public static boolean isPrime(int v)
{
for(int i=(int)Math.sqrt(v); i>1; i--)
{
if((v % i) == 0) return false;
}
return true;
}
测试:
int lower = 10;
int upper = 100;
for(int i=lower; i<=upper; i++)
{
int prime = nearest1Prime(lower, upper, i);
System.out.println("Nearest Prime: " + i + " : " + prime);
}
输出:
10 : 11
11 : 11
12 : 11
13 : 11
14 : 11
15 : 11
16 : 11
17 : 11
18 : 11
19 : 11
20 : 11
21 : 11
22 : 31
23 : 31
24 : 31
25 : 31
26 : 31
27 : 31
28 : 31
29 : 31
30 : 31
31 : 31
32 : 31
33 : 31
34 : 31
35 : 31
36 : 31
37 : 41
38 : 41
39 : 41
40 : 41
41 : 41
42 : 41
43 : 41
44 : 41
45 : 41
46 : 41
47 : 41
48 : 41
49 : 41
50 : 41
51 : 41
52 : 61
53 : 61
54 : 61
55 : 61
56 : 61
57 : 61
58 : 61
59 : 61
60 : 61
61 : 61
62 : 61
63 : 61
64 : 61
65 : 61
66 : 61
67 : 71
68 : 71
69 : 71
70 : 71
71 : 71
72 : 71
73 : 71
74 : 71
75 : 71
76 : 71
77 : 71
78 : 71
79 : 71
80 : 71
81 : 71
82 : 71
83 : 71
84 : 71
85 : 71
86 : 71
87 : 71
88 : 71
89 : 71
90 : 71
91 : 71
92 : 71
93 : 71
94 : 71
95 : 71
96 : 71
97 : 71
98 : 71
99 : 71
100 : 71
英文:
I'm assuming you need a prime satisfying these conditions:
- within a range (lower to upper)
- ending in 1
- nearest to a query value
If so, wouldn't the best strategy be to search outwards in both directions (below and above) and stop when you first hit a prime ending in 1?
public static int nearest1Prime(final int lower, final int upper, final int val)
{
if(val < lower || val > upper) return 0;
int before, after;
if((val % 10) == 1)
{
if(isPrime(val))
return val;
before = val - 10;
after = val + 10;
}
else
{
int base = 10 * (val / 10);
if(val == base)
{
after = base+1;
before = after-10;
}
else
{
before = base+1;
after = before+10;
}
}
int prime = 0;
while(prime == 0 && (before >= lower || after <= upper))
{
if(before >= lower && isPrime(before))
prime = before;
if(after <= upper && isPrime(after) && (prime == 0 || (after-val) < (val-before)))
prime = after;
before -= 10;
after -= 10;
}
return prime;
}
public static boolean isPrime(int v)
{
for(int i=(int)Math.sqrt(v); i>1; i--)
{
if((v % i) == 0) return false;
}
return true;
}
Testing:
int lower = 10;
int upper = 100;
for(int i=lower; i<=upper; i++)
{
int prime = nearest1Prime(lower, upper, i);
System.out.println("Nearest Prime: " + i + " : " + prime);
}
Output:
10 : 11
11 : 11
12 : 11
13 : 11
14 : 11
15 : 11
16 : 11
17 : 11
18 : 11
19 : 11
20 : 11
21 : 11
22 : 31
23 : 31
24 : 31
25 : 31
26 : 31
27 : 31
28 : 31
29 : 31
30 : 31
31 : 31
32 : 31
33 : 31
34 : 31
35 : 31
36 : 31
37 : 41
38 : 41
39 : 41
40 : 41
41 : 41
42 : 41
43 : 41
44 : 41
45 : 41
46 : 41
47 : 41
48 : 41
49 : 41
50 : 41
51 : 41
52 : 61
53 : 61
54 : 61
55 : 61
56 : 61
57 : 61
58 : 61
59 : 61
60 : 61
61 : 61
62 : 61
63 : 61
64 : 61
65 : 61
66 : 61
67 : 71
68 : 71
69 : 71
70 : 71
71 : 71
72 : 71
73 : 71
74 : 71
75 : 71
76 : 71
77 : 71
78 : 71
79 : 71
80 : 71
81 : 71
82 : 71
83 : 71
84 : 71
85 : 71
86 : 71
87 : 71
88 : 71
89 : 71
90 : 71
91 : 71
92 : 71
93 : 71
94 : 71
95 : 71
96 : 71
97 : 71
98 : 71
99 : 71
100 : 71
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论