file access auditing
Results 1 to 2 of 2

Thread: [PHP] Solving Euler Project #*

Threaded View

  1. #1
    Join Date
    Sep 2006
    Posts
    1,649

    [PHP] Solving Euler Project #*

    I decided to join [url]http://projecteuler.net/[/url] which has many math problems that are meant to be solved using programming. I decided to solve the first one and post my code. I could've made it shorter, but I wanted to make it easy to read, and frankly, I don't think saving a few tenths of a second will make much of a difference.

    [PHP]<?php
    /*********************************
    * The Euler Project #* Solution *
    * Coded By Moonbat *
    * July **, 2008 *
    *********************************/
    echo "<center>Getting all the natural number multiples of * and 5 below *000</center><br><br>";
    $multiples = array(); // For keeping hold of all of the multiples
    $counter = 0; // For kicks
    for ($i=*; $i<*000; $i++)
    {
    $multiplethree = $i / *;
    $multiplefive = $i / 5;
    // Checking to see if the answer has a decimal (i.e. not a multiple) of * or 5
    $finalthree = str_replace(".", "<b>THIS IS NOT A MULTIPLE OF THREE</b>", $multiplethree);
    $finalfive = str_replace(".", "<b>THIS IS NOT A MULTIPLE OF FIVE</b>", $multiplefive);
    // If the number doesn't have a decimal, add it to the $multiples array
    if (!preg_match("/THIS IS NOT A MULTIPLE OF THREE/i", $finalthree)) {
    echo "$i is a multiple of * or 5 <br>";
    $multiples[$counter] = $i;
    $counter++;
    } elseif (!preg_match("/THIS IS NOT A MULTIPLE OF FIVE/i", $finalfive)) {
    echo "$i is a multiple of * or 5 <br>";
    $multiples[$counter] = $i;
    $counter++;
    } else {
    echo "";
    }
    }
    // BTW, $wallops == 466 for those who are too lazy to run this code
    $wallops = count($multiples);
    $sum = array_sum($multiples); // Adding the values in the array
    echo "<hr> There are<b> $wallops </b>numbers below *000 that are multiples of * or 5<hr>";
    echo "The sum of all of these numbers is $sum";
    ?>[/PHP]
    If you haven't figured it out already, I love PHP
    Last edited by Moonbat; 07-13-2008 at 04:04 PM.
    "Workers of the world unite; you have nothing to lose but your chains." -Karl Marx

Similar Threads

  1. Euler #6
    By Moonbat in forum Programming
    Replies: 1
    Last Post: 07-26-2008, 11:07 AM
  2. [PHP] Solving Euler Project #2
    By Moonbat in forum Programming
    Replies: 1
    Last Post: 07-24-2008, 05:36 PM
  3. PHP - Euler #5 Problems :(
    By Moonbat in forum Programming
    Replies: 3
    Last Post: 07-18-2008, 02:06 PM
  4. Solving Quadratics with PHP
    By Moonbat in forum Programming
    Replies: 1
    Last Post: 07-12-2008, 02:27 PM
  5. PHP Project Help Needed
    By Moonbat in forum Programming
    Replies: 8
    Last Post: 03-29-2008, 10:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts