# TopCoder is suitable for programming beginners

## Forum: PC programming project euler

hello, do you know projecteuler.net? i think the site is pretty good (as far as i can tell, i'm not far at solving problem 4) and with this i have already encountered a problem, namely checking whether a number is a palindrome or not . i wanted to "just" write a little function, but it somehow doesn't work yet. So if you also want to help program something and want to present your solutions, you can do that here;) and if you can give me a tip why my function doesn't do what it should, you can do that too. have fun solving the problems! lg lukas
 1 intis_palindrome (intx) { 2 // Checks max 10 digit numbers whether they are palindromic. 3 // returns 99 if x is not allowed 4 // returns 1 if x is a palindrome 5 // returns 0 if x is not a palindrome 6 charn = 0; 7 do {// determine the number of digits 8 if (x <10) return1; 9 if (x> 999999999) n = 10; break; 10 if (x> 99999999) n = 9; break; 11 if (x> 9999999) n = 8; break; 12 if (x> 999999) n = 7; break; 13 if (x> 99999) n = 6; break; 14 if (x> 9999) n = 5; break; 15 if (x> 999) n = 4; break; 16 if (x> 99) n = 3; break; 17 if (x> 9) n = 2; break; 18 return99; 19 } while (true); 20 chara [n]; 21 charb [n]; 22 inttmp; 23 for (inti = 0; i 24 a [i] = 0; b [i] = 0; 25 } 26 for (inti = 0; i 27 tmp = 0; 28 for (intj = 0; j 29 tmp + = a [j]; 30 } 31 tmp = x-tmp; 32 a [i] = tmp% int (pow (10, i)); 33 } 34 for (inti = 0; i 35 b [i] = a [n-i]; 36 } 37 for (inti = 0; i 38 if (a [i]! = b [i]) return0; 39 } 40 return1; 41 }

my approach was very simple: convert number to string invert string compare strings;)
 1 boolisPalindromNumber (uint64_tn) 2 { 3 ostringstreamostr; 4 ostr << n; 5 stringst; 6 stringt = ostr.str (); 7 reverse (t.begin (), t.end ()); 8 returnostr.str () == t; 9 }
then just two nested loops that test all combinations. Since there are no runtime requirements and there are only 900 iterations per loop, it can still be done quickly

I would have reassembled the number. (untested)
 1 intis_palindrome (uint64_tzahl) { 2 uint64_tz, rotated = 0; 3 4 for (z = number; z> 0; z / = 10) 5 rotated = rotated * 10 + (z% 10); 6 7 return (rotated == number); 8 }
A 32-bit int can hold values ​​up to almost 2 billion. That is 10 digits, but not quite :-)

If you can't solve it, then you can't solve it. ;) I've spent many days with project euler. Helping each other is definitely not welcome there.

We had some time ago. The Project Euler problems are relatively boring and not demanding, as there are no runtime restrictions and you do not have to try to come up with something clever. I think http://www.spoj.pl/ is much better, but that should be too high for most Ings duck and away

anon wrote:> If you can't solve it, then you can't solve it. ;) >> I've spent many days with project euler. Helping> each other is definitely not welcome there. hello, I didn't ask for a code to copy-paste ... I will continue to work on my solution ... although I like the conversion to string very much;) Thanks for your comments so far! I'll have a look at the other side tonight. sounds interesting, even if it should be difficult, or even because of it. apparently you grow with the challenge :) lg lukas

lukas wrote:> and if you can give me a tip why my function doesn’t do> whatever it should, you can do that too.> a [i] = tmp% int (pow (10, i)); That shouldn't do what you think it should do. But your code is so tangled that it's hard to tell without seeing it in action. Have the arrays output to you, then you can see what comes out and you can continue thinking. I can’t let it go. if you want to divide a number into ones, tens, etc. then that's how it works
 1 i = 0; 2 while (x> 0) { 3 a [i] = x% 10; 4 x = x / 10; 5 ++ i; 6 }
you approach it in a rather complicated and confusing way.

anon wrote:> i have spent many days with project euler. Me too, unfortunately only 40 (??) puzzles solved. > That help> one another is definitely not welcomed there. Psst, just don't tell anyone (or keep saying?). As for problem 4, I took perl, beware of novice programmers (in Perl):
 1 use strict; 2 use warnings; 3 4 my \$ z1; 5 my \$ z2; 6 my \$ val; 7 my \$ valr; 8 my \$ max = 1; 9 10 for \$ z1 (100..999) 11 { 12 for \$ z2 (\$ z1..999) 13 { 14 \$ val = \$ z1 * \$ z2; 15 \$ valr = reverse \$ val; 16 if (\$ val eq \$ valr) 17 { 18 \$ max = \$ val if (\$ val> \$ max); 19 } 20 } 21 } 22 23 print \$ max;
Takes less than a second on my box.

anon wrote:> If you can't solve it, then you can't solve it. ;) >> I've spent many days with project euler. The help> among each other is definitely not welcomed there. It's bullshit. Whether someone helps you or not cannot be controlled anyway. You could also ask a friend, or a fellow student, or a work colleague, ...

so, i have now found a working, not very efficient, solution that can be executed on most computers in less than 5sec. what kind of solution do you get out? I get 90909 out, it should be right, right ??? lg lukas ps spoj.pl also looks very interesting, and the first task definitely makes you want more :)

Vlad Tepesch wrote:> My approach was very simple: >> Convert number to string> Invert string> Compare strings Unfortunately, it is wrong: # 4: Find the largest palindrome made from the product of two 3-digit numbers. In addition: Even if you find the number on a computer, the task is not solved because the proof it is missing that the number found has the required properties.

Johann L. wrote:> Vlad Tepesch wrote: >> my approach was very simple: >>>> convert number to string >> reverse string >> compare strings >> but unfortunately it is wrong: >> # 4: Find the largest palindrome made from the product of two 3-digit> numbers. >> In addition: Even if you find the number on a computer, the> task is not solved because there is no evidence that the number> has the required properties. it was only a question of whether a number is a palindrome or not, and that this function then has to be built into the actual program to solve the problem is clear. lg lukas

and because i'm at it, what do you get out of problem 8? 40824 is my solution. could be true, right? lg lukas

Johann L. wrote:> Unfortunately it is wrong: >> # 4: Find the largest palindrome made from the product of two 3-digit> numbers. I see no contradiction. I iterate quadratically over the numbers 100..999, test for palindrome using the above-mentioned approach and note the largest previous palindrome number.

lukas wrote:> so, i now have a working, not very efficient, but> found solution that can be executed on most computers in less than 5sec. >> what kind of solution do you get out? >> i get 90909 out, but should correct, or ??? not correct. lukas wrote:> and because i'm at it, what do you get out of problem 8? >> 40824 is my solution. could be true, right? fits.

lukas wrote:> I get 90909 out, should be right, right ??? Is wrong by a factor of about 10.