Click here to Skip to main content
15,885,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
********* hi everyone...I solved this problem myself....I put the code below in case anyone might need it...thanks everyone for your help ***********

this code should be written with java

first a number between 1 and 8 should be received(we name it n for ex) then among all the numbers with n digits, we need to print the ones that are prime numbers themselves and if we we keep removing one digit at a time from the end of those numbers, they will still be prime numbers

the number 2399 is a prime number. if we remove the last digit we'll have 239 which is still a prime number. if we remove another digit from the end, we'll have 23 which is still a prime number and by removing another digit we will have 2 which is still a prime number.....so 2399 is one of the numbers that we have to print.

another example: if n=3 these numbers should be printed out: 233 239 293 311 313 317 373 379 593 599 719 733 739 797

What I have tried:

public class u {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int n=input.nextInt();String k="";int b=0;int x=0;input.close();
if(n==1){x=0;}
else{x=Integer.parseInt("1"+new String(new char[n-1]).replace("\0", "0"));}
int y=Integer.parseInt(new String(new char[n]).replace("\0", "9"));
int j=x;int f=0;int d=0;
int i,flag=0;

while(j<=y){
d=0;
for(int t=0;t
Posted
Updated 7-May-22 10:14am
v4
Comments
Greg Utas 6-May-22 17:43pm    
Show us your code then.

Although I use C++, not Java, I doubt you need to use strings. If you divide an integer by 10 in C++, and I assume in Java, it is the equivalent of removing the last digit. It doesn't get rounded up, but truncated.
Patrice T 6-May-22 17:57pm    
Show your code to get help !

Quote:
How do I write a write a cod e that prints out the numbers explained below?

Looks like you took a most complicated way !
Quote:
reversing the string and removing a substring and reversing it again

Substring() can remove the ending chars directly without reversing the string first.
Substring in Java - javatpoint[^]
Quote:
turning the numbers into string and reversing the string and removing a substring and reversing it again and turning it into int again

Why do you turn the integer into string in first place ?
Removing the unit digit of 233 is just dividing by 10.
Integer Division in Java - Studytonight[^]

Java Program to Break Integer into Digits - Javatpoint[^]

Further help is not possible without you source code.
2 3 5 7
23 29 31 37 53 59 71 73 77 79
233 239 293 299 311 313 317 319 371 373 377 379 533 539 593 599 713 719 731 733 737 739 773 779 791 793 797 799
...
79939133 79973939 79993399 79993799 79993937

[Update]
Problems I see with your code are :
- your code is brute force at every step, and this is bad.
- while you check is a number is a prime, you do nothing with the answer, you don't use the prime/not prime result.
- your prime checking algorithm is the most brute force possible. Checking n/2 as divisor means that you expect n to be the product of (n/2) and 2, but 2 is the first divisor you checked.
- If you start with a single digit and then add digits 1 by 1, it is more efficient. a single check of 4 avoid checking 10,000,000 numbers (numbers like 4xxxxxx).
But you have to rewrite the whole code.
Nota: This kind of problems qualify to recursive code.

About your prime checking code, you start by checking small factors 2, 3, 5 ...
checking factor 2 means n= 2* (n/2)
checking factor 3 means n= 2* (n/3)
checking factor 5 means n= 2* (n/5)
...
Then when you check factor (n/2), this means n= (n/2)* 2, and you already know the answer.
Then when you check factor (n/3), this means n= (n/3)* 3, and you already know the answer.
if you study a little integer factorization, you will see that you can stop really earlier, and thus be really faster.
By the way, making a function IsPrime() is a good idea.
 
Share this answer
 
v7
Comments
CPallini 7-May-22 12:59pm    
5.
Patrice T 7-May-22 14:05pm    
Thank you
this is the code I've written.....which is totally wrong


import java.util.Scanner;

public class p {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int n=input.nextInt();
String a=new String(new char[n]).replace("\0", "1");
String b=new String(new char[n]).replace("\0", "8");
int x=Integer.parseInt(a); int y=Integer.parseInt(b);
int i=x;
int p=0;
int r=0;
String v="";
int m=0;
while(i<=y && i>=x){
p=0;
boolean flag = false;
for (int t = 2; t <= i / 2; t++) {
if (i % t == 0) {
flag = true;
break;
}
}
if (!flag){r=1;}
else{r=0;}

for(int q=0;r==1;q++){
String d=Integer.toString(i);
d=new StringBuilder(d).reverse().toString();
d=b.substring(q);
d=new StringBuilder(d).reverse().toString();
int c=Integer.parseInt(d);
flag = false;
for (int j = 2; j <= c / 2; j++) {
if (c % j == 0) {
flag = true;
break;
}
}
if (!flag){r=1;}
else{r=0;}
m=m+1;
}
if (m==n){v=v+Integer.toString(i)+" ";}
i=i+1;
}
System.out.print(v);
input.close();

}

}
 
Share this answer
 
Comments
Patrice T 7-May-22 1:26am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900