programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Left Truncatable Primes Java Program

Rama chandranS
Greenhorn
Posts: 3
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 need help in this ..

Anayonkar Shivalkar
Bartender
Posts: 1558
5
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
• 2
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
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