Problem 044

A place to air possible concerns or difficulties in understanding ProjectEuler problems. This forum is not meant to publish solutions. This forum is NOT meant to discuss solution methods or giving hints how a problem can be solved.
Forum rules
As your posts will be visible to the general public you are requested to be thoughtful in not posting anything that might explicitly give away how to solve a particular problem.

This forum is NOT meant to discuss solution methods for a problem.

In particular don't post any code fragments or results.

Don't start begging others to give partial answers to problems

Don't ask for hints how to solve a problem

Don't start a new topic for a problem if there already exists one


See also the topics:
Don't post any spoilers
Comments, questions and clarifications about PE problems.
JPGargoyle
Posts: 8
Joined: Mon Oct 06, 2008 5:15 pm

Re: Problem 44, need explanation

Post by JPGargoyle »

Thank you very much both :)
(and for your quick answers)

Now I know what to go after ;)


Best regards
userProjectEuler
Posts: 7
Joined: Sat Jul 25, 2009 8:52 pm

Problem 44

Post by userProjectEuler »

missing word: smallest pair in the problem text. The word is present in the list of problems, but not in the the text:
Find the pair of pentagonal numbers, ...

My (horrible) solution took 29 seconds. xD. But now I'm able to see how to do it the right way.

This site is GREAT. =D

edited: I just add a break when find the answer and it took <= 1s. :lol:
Last edited by userProjectEuler on Wed Jul 29, 2009 5:35 am, edited 1 time in total.
User avatar
daniel.is.fischer
Posts: 2400
Joined: Sun Sep 02, 2007 11:15 pm
Location: Bremen, Germany

Re: Problem 44

Post by daniel.is.fischer »

Actually, the text has been changed to what it is now because of complaints about the ambiguity of "smallest pair", so it was made "and D = |Pk − Pj| is minimised" to disambiguate.
Il faut respecter la montagne -- c'est pourquoi les gypa&egrave;tes sont l&agrave;.
User avatar
elendiastarman
Posts: 410
Joined: Sat Dec 22, 2007 8:15 pm

Re: Problem 44

Post by elendiastarman »

EDIT: Found a baaaaad bug in my code...I wasn't dividing by 2.
EDIT2: Yup. Solved it.

*sigh* My code only gives one solution, not matter how long I run it, yet it's not the right answer. My question is, is the difference between the indices of the two pentagonal numbers 19?
Want some
3.14159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679...?
Image
User avatar
rayfil
Administrator
Posts: 1412
Joined: Sun Mar 26, 2006 5:30 am
Location: Quebec, Canada
Contact:

Re: Problem 44

Post by rayfil »

is the difference between the indices of the two pentagonal numbers 19?
NO :(
When you assume something, you risk being wrong half the time.
ccarr
Posts: 3
Joined: Thu Feb 25, 2010 1:19 am

Re: Problem 044

Post by ccarr »

I used a similar method and got the correct answer, although it was awful slow. I still think it's the only way to ensure that the difference is minimized (many used faster algorithms that did not really minimize D, but nevertheless got the solution).
I used a method that I think guarantees that the first solution found is the one with minimal |Pj - Pk| using the fact that Pn+Pn+1 = 3n2 + 2n + 1. As you go through Pn+Pn+2, Pn+Pn+3, ..., Pn+Pn+x, the equations evolve in a constant fashion. The only difficulty is determining what value of "x" is adequate to either accept or reject Pn as the lesser value of the desired pair [Pj, Pk]. I arbitrarily chose 1000, which resulted in an answer within 200ms, but I wonder if there is a more certain way?
Cerno
Posts: 2
Joined: Sat Oct 02, 2010 7:48 pm

Re: Problem 044

Post by Cerno »

For completeness's sake it should be mentioned that the real optimal solution to this problem is trivial:
Expand
P1-P0 = 1-0 = 1 = P1
P1+P0 = 1+0 = 1 = P1
I spent the better part of the day figuring out problem 044 and wondered why my code did not converge until I realized that my function to test for pentagonal numbers was severely flawed:
  • Compute the inverse P-1of P
  • test int(P-1) == P-1.
Really kind of embarrassing (floating point representation FTW)
sstein
Posts: 2
Joined: Tue Aug 10, 2010 2:11 pm

Re: Problem 044

Post by sstein »

I only solved this problem today but I'm really not happy with my solution.

It arrives at the correct number, but I had to guess at the upper limit for generating pentagonal numbers. How do you know that the correct D is actually minimised ?

I can't help but feel I just got lucky on this one.
Image
User avatar
GenePeer
Posts: 112
Joined: Sat Apr 03, 2010 1:14 pm
Contact:

Re: Problem 044

Post by GenePeer »

Don't remember how I solved this at first, so I've been thinking about this for a while, and this is my best way to be sure no better solution exists. I hope this isn't revealing too much.

Once you've found a "minimal pair" (i,j), $D=P_{j}-P_{i}$, then you shouldn't check further than $m=\lfloor\frac{D+2}{3}\rfloor$. Checking any pairs (k,l), for l>m will always produce differences greater than D. So the upper-limit, m, is updated as smaller D's are found. This will give the program O(D2) running time. Knowing how large our current D is though, the program could take around 16hrs to finish. If a smaller D exists, we will find it.

edit: More deductions can be made to reduce the running time, but that would be spoiling the problem. I'll try and run this to see how quick it would be.

edit 2: The required D found in 0.5s, "certainty achieved" in 3s. :)
Image
xe3tec
Posts: 46
Joined: Thu May 05, 2011 8:52 am
Location: Vienna
Contact:

Re: Problem 044

Post by xe3tec »

this problem rly makes me feel stupid :(
It never took me more than 4h to solve a problem, but this time I hv no more good ideas

A question: U hv to precalculate all pentagonal numbers till a certain limit, dont u?
And then u hav to checke "all" possible sums and differences between the numbers...
but this takes just too much time.

so could sb plz gimme a hint?
User avatar
Lord_Farin
Posts: 239
Joined: Wed Jul 01, 2009 10:43 am
Location: Netherlands

Re: Problem 044

Post by Lord_Farin »

I am not inclined to give you a hint just because you haven't solved it fast enough to your liking. I remember being stuck on this problem for weeks and weeks before finally solving it.

Also, I think we (the forum members) would all (or at least mostly) greatly appreciate it if you at least tried to use correct English (as I can see you are capable of doing so), and not use excessive abbreviations. Thanks in advance.
Image
xe3tec
Posts: 46
Joined: Thu May 05, 2011 8:52 am
Location: Vienna
Contact:

Re: Problem 044

Post by xe3tec »

You are absolutely right. I am deeply sorry. Seriously!

You said it took you weeks? I won't sleep if I can't solve it...I guess it's a long night again

edit 10min later:
Congratulations, the answer you gave to problem 44 is correct.

I had it right in the beginning, just optimized something. Easssy
User avatar
Lord_Farin
Posts: 239
Joined: Wed Jul 01, 2009 10:43 am
Location: Netherlands

Re: Problem 044

Post by Lord_Farin »

It indeed took me weeks to realise that there was not an amazingly beautiful solution, and that using Java instead of Mathematica would yield the required iteration power ;)
Image
xe3tec
Posts: 46
Joined: Thu May 05, 2011 8:52 am
Location: Vienna
Contact:

Re: Problem 044

Post by xe3tec »

Yes, I also searched for a better solution but I had no idea. At the end it was 7 lines of python code which took 0,996sec. I think its fast enough.
User avatar
Francky
Posts: 90
Joined: Sat May 07, 2011 3:49 pm
Location: South of France

Re: Problem 044

Post by Francky »

I'm proud of the solution I've found for this problem particulary (the hard version, without bounds !)
Result in 2 second with Python.
Where can I share this solution ?
------

It's my first post, so :
I'm a math teacher in France, with a poor english.
I learned a little Turbo Pascal before my baccalauréat. After only "pure" math.
Now, I rediscover programmation with Python, I like it.
Your site is wonderfull. Thanks for all.
I've translated in french the first 50 problems and I've searched pretty solutions,
I wish this work will help french student to come here.
ImageEntia non sunt multiplicanda praeter necessitatem
xe3tec
Posts: 46
Joined: Thu May 05, 2011 8:52 am
Location: Vienna
Contact:

Re: Problem 044

Post by xe3tec »

Not here. Maybe on your Blog or Website if you have one (like I do).
But you got a PM.
User avatar
Francky
Posts: 90
Joined: Sat May 07, 2011 3:49 pm
Location: South of France

Re: Problem 044

Post by Francky »

I did the real chalenge (|Pj-Pk| minimised, not the sum)
with no bounds, a priori.
--
Find D took 0.95 s
The second D in the list is
?--?8562 (only last digits), and took me 555 s of Python.
ImageEntia non sunt multiplicanda praeter necessitatem
User avatar
GenePeer
Posts: 112
Joined: Sat Apr 03, 2010 1:14 pm
Contact:

Re: Problem 044

Post by GenePeer »

xe3tec wrote:Not here. Maybe on your Blog or Website if you have one (like I do).
That's not a good idea as your Blog or Website is open to public.
Francky wrote:I did the real chalenge (|Pj-Pk| minimised, not the sum)
with no bounds, a priori.
--
Find D took 0.95 s
The second D in the list is
?--?8562 (only last digits), and took me 555 s of Python.
You can PM me your solution, I'm not an admin but just interested in discussing the problem. I also solved it with no bounds, a priori.
Image
User avatar
thedoctar
Posts: 128
Joined: Fri Apr 15, 2011 11:57 am
Location: Sydney, Australia

Re: Problem 044

Post by thedoctar »

Hey, I've just solved the problem, and it took me 1.5 seconds in Python to find the correct solution, though I only guessed the upper limit. Is there any way to mathematically obtain a limit for D? I thought that if P(n+1)-P(n)> Minimum obtained value of D, then you could stop searching, but it takes too long, I think.
Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
Image
fabas indulcet fames
User avatar
Francky
Posts: 90
Joined: Sat May 07, 2011 3:49 pm
Location: South of France

Re: Problem 044

Post by Francky »

There's others solutions, so...
A good algorithm proove that the solution is THE solution, without any bounds !
Mine does the job in 0.96s of uncompiled Python.
The second solution is reached in 555s.
ImageEntia non sunt multiplicanda praeter necessitatem
Post Reply