Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question What is the algorithm to compute the factorisation of an integer?

    Hi,
    I need some ideas on how to compute the factorisation of a non-zero integer?
    For example:
    if the user input is 8, its factorisation is 1 * 2 * 2 * 2;
    if the user input is -300, its factorisation is -1 * 2 * 2 * 3 * 5 * 5

    Does anyone know?

    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,912
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I have this in my library - I do not know where it came from.

    <HTML>
    <HEAD>
    <title>Number Factorer</title>
    <style>
    <!--
    SPAN#calcNotice { container: positioned; position: absolute; top: 150px; left: 200px;
    z-index: 5; background-color: #c0c0c0; border: 5px outset; visibility: hidden }
    -->
    </style>
    <SCRIPT>
    <!--

    function factor() {
    origNum = parseInt( document.forms[0].toBeFactored.value );
    numFac = origNum;
    if ( isNaN( numFac ) || ( numFac <= 0 ) ) {
    alert( "Please enter a non-zero positive integer." );
    } else {
    document.all.facNum.innerText = numFac;
    document.all.calcNotice.style.visibility = "visible";
    factorListHolder = "";
    document.all.factorList.innerText = "";
    PrimeFlag = true; while ( ( numFac % 2 == 0 ) && ( numFac != 2 ) ) {
    PrimeFlag = false;
    factorListHolder = factorListHolder + "2 ";
    numFac = numFac / 2;
    }
    Test = 3;
    maxTest = Math.sqrt( numFac );
    while ( Test <= maxTest ) {
    if ( numFac % Test == 0 ) {
    PrimeFlag = false;
    numFac = numFac / Test;
    factorListHolder = factorListHolder + Test + " ";
    } else {
    Test = Test + 2;
    }
    }
    if ( numFac != 1 ) {
    if ( ( numFac != origNum ) && ( !PrimeFlag ) ) {
    factorListHolder = factorListHolder + numFac;
    }
    }
    if ( PrimeFlag ) {
    document.all.factorList.innerText = "none besides 1 and itself.";
    document.all.prevOutPuts.innerHTML = document.all.prevOutPuts.innerHTML + "Factors of " + origNum + " are none besides 1 and itself.<br>";
    } else {
    document.all.factorList.innerText = factorListHolder;
    document.all.prevOutPuts.innerHTML = document.all.prevOutPuts.innerHTML + "Factors of " + origNum + " are " + factorListHolder + "<br>";
    }
    document.all.calcNotice.style.visibility = "hidden";
    }
    }

    // -->
    </SCRIPT>
    </HEAD>
    <BODY>

    <FORM>
    Factor <input type="text" id="toBeFactored"><button onclick="factor();">Go!</button>
    </FORM>
    <h1>Factorer</h1>
    <p>Factors of <span id="facNum">44100</span> are <span id="factorList">2 2 3 3 5 5 7 7</span></p>

    <h2>Previous Outputs</h2>
    <span id="prevOutPuts"></span>

    <span id=calcNotice><p>Calculating...</p></span>

    </BODY>
    </HTML>

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Yes, we do know.

    Show us what you've got so far and we'll help you out.

    P.S. the above script does not work in Netscape or FireFox.
    Last edited by VortexCortex; 10-30-2006 at 04:13 PM.

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, while we're pasting code examples...
    Here's a script I wrote that returns an array of the factors so you can actually do something with them once the factoring is complete.
    PHP Code:
    <html>
        <
    head>
            <
    title>Prime Factors</title>
        <
    script type="text/javascript">
            function 
    getFactors(num){
                var 
    n//Variable to store the current number.
                
    var = new Array(); //Array to hold the factors.
                
    if(!isNaN(parseInt(num10))&&(!= 0)){ //Convert n into an integer and make sure num is a number and non zero.
                    
    f[f.length] = / (Math.abs(n)); //Make first element either 1 or -1 and convert n to a positive number for faster factoring.
                    
    var 2//Variable to hold current factor.
                    
    while(n){
                        if (
    == 0){ //i is a factor of n
                            
    /= i//divide n by its factor.
                            
    f[f.length]=i//store the factor.
                            
    2//start over at factor of 2.
                        
    } else if (2i+=2//Avoid even numbers since 2 is the only even prime.
                        
    else i=3//try 3 as a factor.
                    
    }
                    
    f[f.length]=n//i now equals n, therefore n is a factor of num.
                
    }
                return 
    f//return the array of factors.
            
    }
        
    </script>
        </head>
        <body>
            <h1>Prime Factors</h1>
            Number to factor:<input id="num" size="10"><button onclick="alert('Prime Factors:\n'+getFactors(document.getElementById('num').value));">Get Factors</button>
        </body>
    </html> 
    Last edited by VortexCortex; 10-30-2006 at 04:08 PM. Reason: added comments


  •  

    Posting Permissions

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