• Post Reply Bookmark Topic Watch Topic
  • New Topic

Left Truncatable Primes Java Program  RSS feed

 
Rama chandranS
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I have this question from euler


The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
i don't know about this program

I need help in this ..
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!
Rama chandranS wrote:Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

So? What's the problem?

Let's start from the basics - What this problem has anything to do with Java? Is it that you want to write a program in Java? Because you are not mentioning it anywhere.

Further to that, this is NotACodeMill. Please share with us what you have tried, and where you are currently stuck/confused et-cetera. It would help other readers as well.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fun of Project Euler is figuring out the solution yourself. It would be very boring if someone gave you the answer and you just fill it in on the website, it would not give you the satisfaction of solving the puzzle.

So, what's the problem - is there something that you don't understand about the puzzle? Did you write some code but you get stuck on it somewhere?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rama chandranS wrote:
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.


Interestingly, this can be easily brute forced. I coded and solved the problem in about 20 minutes. Grab an old isPrime() method. Wrote a truncateLeft() method, as truncate right is incredibly easy. Wrote a check for truncate left primes. Wrote a check for truncate right primes. And did a brute force loop from 11 up (single digits are not truncate-able).

The question actually helps greatly here. It tells you that there are only eleven values, so you have an end point.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!