Junilu Lacar wrote:Since you're dealing with integer values, you can just use % and / (integer division) to reverse the number. If the reverse of a number and the original number are the same then it's a palindromic number.
For example, the reverse of 89:
89 % 10 = 9
89 / 10 = 8
9 * 10 = 90
90 + 8 = 98
98 != 89 therefore 89 is not a palindrome
77 % 10 = 7
77 / 10 = 7
7 * 10 = 70
70 + 7 = 77
77 == 77 therefore 77 is a palindrome
---
Exercise for you is to generalize this and put it into a loop.
I would break the problem down into two parts:
1 - find the reverse of a number
2 - find the largest palindromic number that is the product of X-digit numbers
Liutauras Vilda wrote:
3. In case it is, check if product (number) has factors
Junilu Lacar wrote:
Liutauras Vilda wrote:
3. In case it is, check if product (number) has factors
I'm curious, what is this going to get you?
The checking for products is an O(n * n) problem - you have to set up a nested for-loop.
Paul Clapham wrote:Besides which, it's actually necessary to check that the number has factors which are both < 1000.
Junilu Lacar wrote:I'm curious, what is this going to get you?
Yes, that would be O(n^n). Not sure if would be quicker actually to work it out with %, as in this case numbers are quite big, using % could be slow.Junilu Lacar wrote:The checking for products is an O(n * n) problem - you have to set up a nested for-loop.
Liutauras Vilda wrote:using % could be slow.
Of course you're right here. My mistake.Junilu Lacar wrote:(n^n) != (n*n)
But that second point is basically the same what I wroteJunilu Lacar wrote:I would break the problem down into two parts:
1 - find the reverse of a number
2 - find the largest palindromic number that is the product of X-digit numbers
Earlier me wrote:3. In case it is, check if product (number) has factors
John Sing wrote:"!Find the largest palindrome made from the product of two 3-digit numbers."
The number I am getting from this code is "580085" which does fulfill the requirements but it's the wrong answer. How can I fix my code to get the right answer?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:Winston,
I may be reading your response wrong but here are a few things I'd point out:
1. The requirement is to find the largest palindromic product of two 3-digit numbers.
So, the range of possible solutions is [100*100 .. 999*999] or [10,000 .. 998,001]
2. That means that you'll have a mix of odd and even numbered digits in the range of possible solutions
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:1. Iterate through all possible products, stop at the first palindromic number that is the product of two N-digit numbers.
2. Iterate through all possible factors and keep track of the largest palindromic product.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:Keep up, son!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Lengths up to 8 take less than a second.
Winston Gutkowski wrote:Takes approx 6 seconds on my laptop...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:
Winston Gutkowski wrote:Lengths up to 8 take less than a second.
I want to see some proof, otherwise it's just hearsay!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:I want to see some proof, otherwise it's just hearsay!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:A proof for n odd is easy, albeit very tedious.
So, an example is much better.
Take n = 7, and look at the number 5866663.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:However, my intention was to get two bounding divisors as fast as possible,
and as close as possible, and for n even the 9...9 and 9...1 are much closer together.
Anyway: the nicest problem I dealt with at PE is problem 84. Thoroughly recommended!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|