SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2013
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    product of prime

    how do i get product of prime example for number 12 in php? in calculation we get product of prime for 12 is 2^2 x 3.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    What is the algoritm you use to get to that answer?
    Once you know that, all you have to do is program it.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2013
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What is the algoritm you use to get to that answer?
    Once you know that, all you have to do is program it.
    hi thanx for replying. what i mean is, is there any code in php that can generate for example (3 x 3) for 9? it's like when we set a variable with a number 9, and the output would be 3 x 3. is there any? if not, any idea how to do it?

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Marvin_G View Post
    hi thanx for replying. what i mean is, is there any code in php that can generate for example (3 x 3) for 9? it's like when we set a variable with a number 9, and the output would be 3 x 3. is there any? if not, any idea how to do it?
    As guido2004 said, if you know the algorithm you can write the code:

    PHP Code:
    $prime = array(2357111317192329);

    $input 9;
    $test $input;
    $result = array();

    foreach(
    $prime AS $value) {
        while (
    $test $value == 0) {
            
    $result[] = $value;
            
    $test $test $value;
            if(
    $value $test) { break 2; }
        }
    }

    echo 
    "input= {$input}<br>\n";
    echo 
    "result= \n<pre>\n";
    print_r ($result);
    echo 
    "</pre>\n"
    Denny Schlesinger
    web services

  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by captainccs View Post
    As guido2004 said, if you know the algorithm you can write the code:

    PHP Code:
    $prime = array(2357111317192329);

    $input 9;
    $test $input;
    $result = array();

    foreach(
    $prime AS $value) {
        while (
    $test $value == 0) {
            
    $result[] = $value;
            
    $test $test $value;
            if(
    $value $test) { break 2; }
        }
    }

    echo 
    "input= {$input}<br>\n";
    echo 
    "result= \n<pre>\n";
    print_r ($result);
    echo 
    "</pre>\n"
    Unfortunately this is not the best approach. Your'e using a static representation of what a prime number is rather than calculating it out. While it will be more expensive as you go higher, this will be more dynamic:
    http://tournasdimitrios1.wordpress.c...bers-with-php/

  6. #6
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Unfortunately this is not the best approach. Your'e using a static representation of what a prime number is rather than calculating it out.
    I was thinking exactly that! My next version won't need that static set.
    Denny Schlesinger
    web services

  7. #7
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Calculating prime numbers from scratch gets to be pretty slow as the numbers get bigger. A practical algorithm would store the already calculated ones and it would add to the table as needed. An interesting problem.
    Denny Schlesinger
    web services

  8. #8
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    A couple of PHP's "built-in" functions between them may well cover what your intending to do:

    * gmp_nextprime() that will find the next prime number after the number given, it could be run in a loop with the function being given the last prime number it found as the number given.
    * gmp_prob_prime() will tell you if the number is probably a prime number.

    I think the former would be your better bet being run in a loop to get an array of prime numbers then that array would be looped through and the product for each one calculated.

    You might find that you have to enable the php_gmp extension first.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  9. #9
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I've now written code that calculates the prime factors of any number and it also calculate the prime numbers it needs. Because calculating prime numbers is very time consuming, it saves the prime numbers for later reuse.

    See The Learning Script
    Denny Schlesinger
    web services


Bookmarks

Posting Permissions

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