I'm a professional game developer from Wakefield, England, working as a senior programmer for Rebellion North.
I'm a married father of five and I a also sometimes do Retroburn stuff.
Martin 'Bytrix' Caine
Father. C++ Games Programmer. Cyclist. Guitarist.
emailfacebooktwittermessengersteamxboxliveretroburn
Tags
2013 3d alphalabs amazon apple archivirtual asynchronous battlefield bad company 2 ben 10 bepu beta blackmagic design blog blue marble bootcamp borderlands bsp calibration charity charvel childsplay comments competition content tracker counter-strike crash csgo css3 cycling dear esther deferred deus ex develop conference direct x discipline documentation doom 3 bfg dpi dr bott eidos elite force email deliverability eurogamer expo facebook focus fresnel game development game horizon game republic gamedev games gaming geoip girls make games global offensive grid guitar half-life 2 hawken hd7 hobbyist htc humble indie bundle imac indie indie trials indietrials intensity pro ip-countryside iron man 3 jamulus rift jquery kids kinect launch conference left 4 dead live lost mac mac osx manchester manhacks mass effect 2 matrox maya minecraft mirrors edge montreal morrowind movies museum of the microstar music mxo2 mini mysql nausea network networking nokia normal mapping obj oculus rift omnitrix ouya pedal for pounds php physics playstation suite port25 portal portal 2 positron posters powermta project aedra project euler promotion properties proton pulse ps vita ps4 psn racer reddit rendering retroburn game studios reviews rift racer riftracer roadkill roller coaster sdl2 shadow racers sharks shoct skyrifters snds space cadet spam trap star trek steam stencyl storage super stock sd1 fr superhot team fortress 2 tesselating tesselation texture editor thunderbird thunderclap ticktock games tiga track builder track bulder trials tv twitter uk ultimatrix usergroup vequencer video vireio visual assist visual studio vorpx voucher vr vr cinema war thunder warren web willow windows 8 windows 8.1 windows phone 7 workbench wp7 wp7dev xbla xblig xblig network xbox xbox live indie games xna xnaukug xperia play zombies on the holodeck
Archive
Links
Web
XNA
Games
Email Deliverability
Monday, May 31st 2010 / Web

Project Euler - problem 4

Another pretty easy one from Project Euler, puzzle 4 simply required building an array of values based on calculations and sorting them to find the highest values:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

Find the largest palindrome made from the product of two 3-digit numbers.


I basically had two variables looping from 999 to 100, multiplying them together for each value. I then converted the value to a string and reversed it, if the reversed string equals the original string the number is palindromic so I added it to an array (making sure to add the number and not the string or ordering the array would work alphabetically rather than numerically).

I then reversed the order of the array and output the first element:


<?
/******************************************************************
*  ProjectEuler.net  *  Problem 4  *  2010-05-31  *  Martin Caine *
******************************************************************/

//create an array to store the results
$numbers = array();

// loop from 999 to 100
for( $x=999$x>99$x-- )
{
    
// loop the second value from 999 to 100
    
for( $y=999$y>99$y-- )
    {
        
$number $x $y;
        
// test if the number is palindromic
        
if( strval$number ) == strrevstrval$number ) ) )
        {
            
// if it's not already in our array, add it now
            
if( !in_array$number$numbers ) )
            {
                
$numbers[] = $number;
            }
        }
    }
}

// sort the numbers in reverse order
rsort$numbers );

// output the highest number
echo $numbers[0];

?>



If you found this post helpful please leave a comment below:
 
Comments
Tags:   php   project euler   web
0