Project Euler

Project Euler

Posers and Puzzles

Cookies help us deliver our Services. By using our Services or clicking I agree, you agree to our use of cookies. Learn More.

P
Upward Spiral

Halfway

Joined
02 Aug 04
Moves
8702
29 Sep 09

Originally posted by iamatiger
But the question asks for the largest such palindrome which is
913 * 993 = 906609
As my perl above finds.
I made a mistake, then. Let me see what went wrong.

Joined
26 Apr 03
Moves
26771
29 Sep 09

Oops, sorry. Edited my post to say what was wrong, but you posted your reply while I was doing that.

P
Upward Spiral

Halfway

Joined
02 Aug 04
Moves
8702
29 Sep 09
1 edit

Ok, so a bit brute force in this unelegant version.

x = 900:999;
a = kron(x,x)';
a1 = reshape(a,numel(a),1);
b = num2str(a1);
c = str2num(fliplr(b)) - a1;
prod = max(a1(find(c==0)));
e = prod./x - floor(prod./x);
num1 = max(x(find(e==0)));
num2 = prod/num1;
display([num1 num2 prod]);

That's 0.68 seconds, result 993 * 913 = 906609. If do the range starting at 100 and calculate everything, but that takes 52 seconds. It wouldn't be hard to do a loop that increased the range if nothing was found. It's funny how the kronecker product is instant (even for the whole range) and what takes time is finding the palindromes. I had to convert them to strings to reverse them. Strings are not Matlab's forte.

P
Upward Spiral

Halfway

Joined
02 Aug 04
Moves
8702
29 Sep 09

Ok, new version:

x = 100:999;
a = kron(x,x)';
a1 = sort(reshape(a,numel(a),1),'descend'😉;
finish = 0;
index = 0;

while finish == 0

index = index+1;
temp1 = num2str(a1(index));
test = str2double(fliplr(temp1)) - a1(index);

if test == 0
prod = a1(index);
temp2 = prod./x - floor(prod./x);
num1 = max(x(find(temp2==0)));
num2 = prod/num1;
display([num1 num2 prod]);
finish = 1;
end

end

This gives me the result in less than one second (0.87 secs). Using the kronecker and sorting descendingly, I get the results ordered by product size. 🙂

P
Upward Spiral

Halfway

Joined
02 Aug 04
Moves
8702
29 Sep 09
1 edit

I just did one for this:
Find a quadratic formula that produces the maximum number of primes for consecutive values of n.

Will post just tomorrow in case somebody wants to give it a go.

Joined
26 Apr 03
Moves
26771
30 Sep 09
3 edits

did that one, 18 secs in perl

Joined
26 Apr 03
Moves
26771
30 Sep 09

Sped it up a bit, takes 3 secs in perl now

P
Upward Spiral

Halfway

Joined
02 Aug 04
Moves
8702
01 Oct 09

Originally posted by iamatiger
Sped it up a bit, takes 3 secs in perl now
That's pretty good. For what range of parameter values?

P
Upward Spiral

Halfway

Joined
02 Aug 04
Moves
8702
01 Oct 09
4 edits

Originally posted by Palynka
That's pretty good. For what range of parameter values?
Just noticed that in the page they restrict the problem to quadratic forms:

n^2+a.n+b, where |a|<1000,|b|<1000. Is that what you did?.

S
BentnevolentDictater

x10,y45,z-88,t3.1415

Joined
26 Jan 03
Moves
1644
01 Oct 09

Originally posted by iamatiger
I excluded 1 because 1 is not a prime number.

http://wiki.answers.com/Q/Why_is_1_not_a_prime_number
giggle out loud. ooooops. So what you are saying here is that old worn out carpenters who barely managed sixth grade maths should not engage in "primes" and whatsits?😛

Joined
26 Apr 03
Moves
26771
01 Oct 09

Originally posted by Palynka
Just noticed that in the page they restrict the problem to quadratic forms:

n^2+a.n+b, where |a|<1000,|b|<1000. Is that what you did?.
I sped that problem size up to about 1 second now, the solution for |a|<10000, |b|<10000 takes 104 seconds.

Joined
26 Apr 03
Moves
26771
01 Oct 09

Originally posted by StarValleyWy
giggle out loud. ooooops. So what you are saying here is that old worn out carpenters who barely managed sixth grade maths should not engage in "primes" and whatsits?😛
No shame in not knowing about 1 not being prime. It's just a convention, no one can prove it's right.

P
Upward Spiral

Halfway

Joined
02 Aug 04
Moves
8702
01 Oct 09

Originally posted by iamatiger
I sped that problem size up to about 1 second now, the solution for |a|<10000, |b|<10000 takes 104 seconds.
That's pretty impressive. What do you use as shortcuts?

Joined
26 Apr 03
Moves
26771
02 Oct 09

I have more radical shortcuts in mind, which would probably let me check a/b combos up to at least 100,000 in reasonable runtime. But the current ones are:

equation is:
n^2 + an + b = some_prime

when n=0, this becomes
b = some_prime

note that if we want a long run, then b can't be 2 because if n was even we would have:
even + even + even = some_prime
the prime would have to be 2, so we would have:
n^2 + an + 2 = 2
n^2 = -an
a = -n
so we would only have a max run length of 1

Therefore we have proved b must be odd and prime, a can't be even because if it was, then when n was odd we would have:
odd + even + odd = prime
which would mean that when n was odd the prime would have to be 2
giving n^2 + an + b = 2
giving only a couple of n's that would work for any given a and b

So, we have proved that b must be a prime number and a must be odd. If we only consider feasible values of a and b then we speed our search up drastically.

The further speed ups I am considering are:
when n = b we get
n^2 + an + n = prime, which is clearly false. Since we know b must be prime and therefore positive, the max run length for any given b is b, therefore we only need to consider b/s bigger than max_run_so_far

Furthermore, the equation can be rearranged to:
a = (prime - b)/n - n
this means, if we are considering a given b, we can more drastically constrain the a's we consider for that b to the ones that are potential integral solutions of that equation for all n's up to max_run_so_far

P
Upward Spiral

Halfway

Joined
02 Aug 04
Moves
8702
03 Oct 09

Originally posted by iamatiger
I have more radical shortcuts in mind, which would probably let me check a/b combos up to at least 100,000 in reasonable runtime. But the current ones are:

equation is:
n^2 + an + b = some_prime

when n=0, this becomes
b = some_prime

note that if we want a long run, then b can't be 2 because if n was even we would have:
even + even + even = some ...[text shortened]... nes that are potential integral solutions of that equation for all n's up to max_run_so_far
Still, I did the b being prime and a being odd but it's still slow. I also have a lower bound for a (it has to be at least >-b), which is still excessive but it cuts some possibilities.

Despite this, it still takes a bit more than 100 secs for the <|1000| ones...