zabbix
+ Reply to Thread
Results 1 to 4 of 4

Thread: PHP - Euler #5 Problems :(

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

    PHP - Euler #5 Problems :(

    First off, here's the problem: "What is the smallest number divisible by each of the numbers * to 20?"

    I went with the good ol' brute-force method. But after trying every combination of loops, if statements, etc. I can't get a working solution. Here's what I'm using so far.
    [PHP]<?php

    set_time_limit(0);
    for ($i=0; $i<=*000000; $i++) {
    if (($i &#*7; 2) == 0 && ($i % *) == 0 && ($i % 4) == 0 && ($i % 5) == 0 && ($i % 6) == 0 && ($i % 7) == 0 && ($i % 8) == 0 && ($i % *) == 0 && ($i % *0) == 0 && ($i % **) == 0 && ($i % *2) == 0 && ($i % **) == 0 && ($i % *4) == 0 && ($i % *5) == 0 && ($i % *6) == 0 && ($i % *7) == 0 && ($i % *8) == 0 && ($i % **) == 0 && ($i % 20) == 0) {
    echo "<h2>" . $i . "</h2>";
    echo "<br>";
    } else {
    echo "";
    }
    }

    ?>[/PHP]
    Even though my for loop goes up to *,000,000, be assured I've tried up to 5,000,000. I doubt the smallest number that can be divided evenly by *-20 is any bigger than *,000,000. What is wrong with my script?
    "Workers of the world unite; you have nothing to lose but your chains." -Karl Marx

  2. #2
    Join Date
    Jun 2006
    Posts
    459
    Sorry to hax you but

    Rather than doing **.6M iterations why not:

    - find prime factors of each number - you nearly did this

    - take the smallest necessary collection of those prime numbers (the highest power of each prime)

    - multiply them up

    And bingo: you have:

    2*2*2*2 * *** * 5 * 7 * ** * ** * *7 * ** = 2*2,7*2,560

    Finding prime factors is easy, and I think this would be more scalable
    7h* L**7*57 c4n7 h4ck m*!
    Proud to have quit playing ®µÑȧ©ÅÞË

    If you write like a semi-literate boob you will very likely be ignored.
    Writing like a l**t script kiddie hax0r is the absolute l**t*st way to write!
    L0L

  3. #3
    Join Date
    Sep 2006
    Posts
    1,649
    Thanks for the idea. I'll try to get it working
    "Workers of the world unite; you have nothing to lose but your chains." -Karl Marx

  4. #4
    Join Date
    Jun 2006
    Posts
    459
    The Principle of Parsimony - It is pointless to do with more what is done with less
    7h* L**7*57 c4n7 h4ck m*!
    Proud to have quit playing ®µÑȧ©ÅÞË

    If you write like a semi-literate boob you will very likely be ignored.
    Writing like a l**t script kiddie hax0r is the absolute l**t*st way to write!
    L0L

+ Reply to Thread

Similar Threads

  1. Euler #6
    By Moonbat in forum Programming
    Replies: 1
    Last Post: 07-26-2008, 10:07 AM
  2. [PHP] Solving Euler Project #2
    By Moonbat in forum Programming
    Replies: 1
    Last Post: 07-24-2008, 04:36 PM
  3. [PHP] Solving Euler Project #*
    By Moonbat in forum Programming
    Replies: 1
    Last Post: 07-13-2008, 04:38 PM
  4. IP problems/need some help!
    By scooterdog in forum Internet Privacy
    Replies: 5
    Last Post: 05-08-2007, 03:14 AM
  5. I have 2 PROBLEMS!!
    By heft in forum Internet Privacy
    Replies: 3
    Last Post: 02-06-2006, 11:46 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