JQuery's method of finding a full number within n
This example describes how to find the full number of n in jQuery. Share it with you for your reference. The specific analysis is as follows:
The perfection of a number depends on its factor (the number that can divide the original number ).
For example, the factor of 12 is 1, 2, 3, 4, and 6. When the sum of the factors of a number is greater than the number itself, the number is called the "profit" number. Therefore, 12 is an integer because the total factor is 16. On the other hand, when the sum of the factors of a number is smaller than the number itself, the number is called a "loss. Therefore, 10 is a loss because the factors (and 5) add up to 8.
The most significant and rare number is the sum of the factors exactly equal to its own number, which is the full number.
-- Ferma's big Theorem
Looking for a full number, we must first calculate the number factor. Baidu will review what is a factor.
Factor: If the integer n is divided by m and the result is an integer without a remainder, m is the factor of n. Note that this relationship is valid only when the divisor, divisor, and quotient are all integers and the remainder is zero. In turn, we call n a multiple of m.
?
1 2 3 4 5 6 7 8 9 10 11 12 |
<! DOCTYPE html> <Html> <Head> <Meta charset = "UTF-8"> <Title> JS Bin </title> </Head> <Body> <Input type = "text" id = "num"/> <Button id = "calc"> calculation </button> <P id = "result"> </p> </Body> </Html> |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
/* * Find the perfect number within n */ Function $ (id ){ Return document. getElementById (id ); } // Determine whether it is a positive integer Function isIntNum (number ){ Var num = number; If ((! IsNaN (num) & (parseInt (num) = parseFloat (num ))){ Return true; } Else { Return false; } } $ ("Calc"). addEventListener ("click", function (){ Var inputNum = $ ("num"). value, $ Result = $ ("result "), FactorArr = [], ResultArr = [], I = 0, J = 0, Sum = 0; // Check whether the input is a positive integer. If (isIntNum (inputNum )){ Console. log ("right "); } Else { $ Result. innerHTML = "input error: enter a positive integer "; Return false; } // Traverse all numbers For (var k = 1; k <inputNum; k ++ ){ // Reset the variable for each calculation FactorArr. length = 0; Sum = 0; // Find the factor of the current number For (I = 1; I <Math. floor (k/2) + 1; I ++ ){ If (k % I = 0 ){ FactorArr. push (I ); } } // Calculate the sum of Factors For (var m = 0; m <factorArr. length; m ++ ){ Sum + = factorArr [m]; } // If the sum of the factor and the value is equal to the current number, the value complies with the full number standard. If (sum = k ){ ResultArr. push (k ); } } $ Result. innerHTML = resultArr; }); |
I hope this article will help you with jQuery programming.