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.