[ 자바스크립트 ] 소수와 합성수 🕔 2016. 11. 8. 13:49
최종 수정일 : 2016. 11. 15.
[ Javascript ] prime number & composite number
소수 ( prime number )
1보다 큰 수 중에서, 1과 그 자신외에는 약수를 가지지 않는 자연수.
1과 자기 자신 외에는 나눌 수 없는 숫자. 더 이상 분해할 수 없는 수.
A natural number greater than 1 that has no positive divisors other than 1 and itself.
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, ...
합성수 ( composite number )
소수가 아닌 자연수. 1과 그 자신 이외의 수를 약수로 가지는 자연수.
합성수는 소수로 나눌 수 있다.
A natural number greater than 1 that is not a prime number.
A positive integer that can be formed by multiplying together two smaller positive integers.
A positive integer that has at least one divisor other than 1 and itself.
4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, ...
1은 소수도, 합성수도 아니다. ( Number 1 is neither prime nor composite. )
☞ Why is the number one not prime? (The Prime Pages)
☞ Prime number - Primality of one (Wikipedia)
- isPrimeNumber
- isPrimeNumber & getPrimes
- isPrimeNumber & choosePrimes
- isCompositeNumber
- isCompositeNumber & getComposites
- isCompositeNumber & chooseComposites
isPrimeNumber()
// source : https://tonks.tistory.com/171#_javascript_prime_&_composite
function isPrimeNumber ( number ){
var floor = Math.floor( number );
if ( isNaN(floor) || floor == Infinity || floor != number || floor < 2 ){
return false;
}
var floorSqrt = Math.floor( Math.sqrt( floor ) );
var x = 2;
for ( x; x <= floorSqrt; x++ ){ if ( floor % x == 0 ) return false; }
return true;
}
<button onclick="testing()"> Click me </button>
<p id="demo1"> </p>
<script type="text/javascript">
function testing (){
var number = "47";
var check = isPrimeNumber( number );
demo1.innerHTML = check;
}
</script>
|
<button onclick="testing()"> Click me </button>
<p id="demo2"> </p>
<script type="text/javascript">
function testing (){
var number = 48;
var check = isPrimeNumber( number );
demo2.innerHTML = check;
}
</script>
|
isPrimeNumber() & getPrimes()
function getPrimes ( max, min ){
var primes = [ ];
if ( isNaN(max) ){ return primes; }
var min = Number( min ) || 2;
var max = Number( max );
if ( min > max ){
var temp = min;
min = max;
max = temp;
}
min = Math.ceil( min );
max = Math.floor( max );
if ( max < 2 ){ return primes; }
if ( min < 2 ){ min = 2; }
/*
* 생성되는 배열의 최대 길이를 100,000으로 설정함.
* I've set up the maximum length of a returning array to the 100,000.
*/
var limit = Math.pow( 10, 5 );
for ( min; min <= max; min++ ){
if ( primes.length === limit ){ return primes; }
if ( isPrimeNumber( min ) ){ primes.push( min ); }
}
return primes;
}
<button onclick="testing()"> Click me </button>
<p id="demo1"> </p>
<script type="text/javascript">
function testing (){
var array = getPrimes( 19.4 );
demo1.innerHTML = array.join( ", " );
}
</script>
|
<button onclick="testing()"> Click me </button>
<p id="demo2"> </p>
<script type="text/javascript">
function testing (){
var array = getPrimes( 19.7 );
demo2.innerHTML = array.join( ", " );
}
</script>
|
<button onclick="testing()"> Click me </button>
<p id="demo1"> </p>
<script type="text/javascript">
function testing (){
var array = getPrimes( "3", 17 );
demo1.innerHTML = array.join( ", " );
}
</script>
|
<button onclick="testing()"> Click me </button>
<p id="demo2"> </p>
<script type="text/javascript">
function testing (){
var array = getPrimes( "17", 3 );
demo2.innerHTML = array.join( ", " );
}
</script>
|
한번 실행될 때 만들어지는 소수의 갯수는 최대 10만 개까지 가능함.
The prime numbers of creating, are up to 100,000 per one time.
<button onclick="testing()"> Click me </button> <div id="demo"> </div> <script type="text/javascript"> function testing (){ var arr = getPrimes( 1299800 ); var last = arr.length - 1; var text = arr[0] + ", " + arr[1] + ", " + arr[2] + ", " + arr[3] + ", " + arr[4] + ", " + arr[5] + ", " + arr[6] + ", " + arr[7] + ", "; text += "... 중간 생략 (omitting this part from array) ... , "; text += arr[ last-2 ] + ", " + arr[ last-1 ] + ", " + arr[ last ]; demo.innerHTML = text; } </script>
<button onclick="testing()"> Click me </button>
<p id="demo"> </p>
<script type="text/javascript">
function testing (){
var array = getPrimes( 1299670, 1299800 );
demo.innerHTML = array.join( ", " );
}
</script>
isPrimeNumber() & choosePrimes()
function choosePrimes ( array ){ var primes = [ ]; var n = 0, value; for ( n; n < array.length; n++ ){ value = array[ n ]; if ( isPrimeNumber( value ) ){ primes.push( value ); } } return primes; }
<button onclick="testing()"> Click me </button>
<p id="demo"> </p>
<script type="text/javascript">
function testing (){
var myArray = [ 1, 2, 3, 4, 5, 6, 7 ];
var array = choosePrimes ( myArray );
demo.innerHTML = array.join( ", " );
}
</script>
isCompositeNumber()
function isCompositeNumber ( number ){ var floor = Math.floor( number ); if ( isNaN(floor) || floor == Infinity || floor != number || floor < 4 ){ return false; } var floorSqrt = Math.floor( Math.sqrt( floor ) ); var x = 2; for ( x; x <= floorSqrt; x++ ){ if ( floor % x == 0 ) return true; } return false; }
<button onclick="testing()"> Click me </button>
<p id="demo1"> </p>
<script type="text/javascript">
function testing (){
var number = 47;
var check = isCompositeNumber( number );
demo1.innerHTML = check;
}
</script>
|
<button onclick="testing()"> Click me </button>
<p id="demo2"> </p>
<script type="text/javascript">
function testing (){
var number = "48";
var check = isCompositeNumber( number );
demo2.innerHTML = check;
}
</script>
|
isCompositeNumber() & getComposites()
function getComposites ( max, min ){ var composites = [ ]; if ( isNaN(max) ){ return composites; } var min = Number( min ) || 4; var max = Number( max ); if ( min > max ){ var temp = min; min = max; max = temp; } min = Math.ceil( min ); max = Math.floor( max ); if ( max < 4 ){ return composites; } if ( min < 4 ){ min = 4; } /* * 생성되는 배열의 최대 길이를 100,000으로 설정함. * I've set up the maximum length of a returning array to the 100,000. */ var limit = Math.pow( 10, 5 ); for ( min; min <= max; min++ ){ if ( composites.length === limit ){ return composites; } if ( isCompositeNumber( min ) ){ composites.push( min ); } } return composites; }
<button onclick="testing()"> Click me </button>
<p id="demo1"> </p>
<script type="text/javascript">
function testing (){
var array = getComposites( 18.4 );
demo1.innerHTML = array.join( ", " );
}
</script>
|
<button onclick="testing()"> Click me </button>
<p id="demo2"> </p>
<script type="text/javascript">
function testing (){
var array = getComposites( 18.7 );
demo2.innerHTML = array.join( ", " );
}
</script>
|
<button onclick="testing()"> Click me </button>
<p id="demo1"> </p>
<script type="text/javascript">
function testing (){
var array = getComposites( 8, 21 );
demo1.innerHTML = array.join( ", " );
}
</script>
|
<button onclick="testing()"> Click me </button>
<p id="demo2"> </p>
<script type="text/javascript">
function testing (){
var array = getComposites( 8.4, 21.7 );
demo2.innerHTML = array.join( ", " );
}
</script>
|
<button onclick="testing()"> Click me </button> <div id="demo"> </div> <script type="text/javascript"> function testing (){ var array = getComposites( 45 ); var n = 0; var length = array.length; var table = "<table border='1' cellspacing='0'><tbody><tr>"; var td = ""; for ( n; n < length; n++ ){ td = "<td style='padding: 4px;text-align: right;'>" + array[ n ] + "</td>"; if ( (n+1) % 10 == 0 ){ td += "</tr>"; } table += td; } table += "</tbody></table>"; demo.innerHTML = table; } </script>
isCompositeNumber() & chooseComposites()
function chooseComposites ( array ){ var composites = [ ]; var n = 0, value; for ( n; n < array.length; n++ ){ value = array[ n ]; if ( isCompositeNumber( value ) ){ composites.push( value ); } } return composites; }
<button onclick="testing()"> Click me </button>
<p id="demo"> </p>
<script type="text/javascript">
function testing (){
var myArray = [ 1, 2, 3, 4, 5, 6, 7 ];
var array = chooseComposites ( myArray );
demo.innerHTML = array.join( ", " );
}
</script>
<script type="text/javascript">
// source : https://tonks.tistory.com/171#_javascript_prime_&_composite
var NaturalNumber = {
limit : Math.pow( 10, 5 ), // 100,000
setRange : function ( a, b, minimum ){
var min = Number( a ) || minimum;
var max = Number( b );
if ( min > max ){
var temp = min;
min = max;
max = temp;
}
min = Math.ceil( min );
max = Math.floor( max );
return { min: min, max: max };
},
isPrime : function ( number ){
var floor = Math.floor( number );
if ( isNaN(floor) || floor == Infinity || floor != number || floor < 2 ){
return false;
}
var floorSqrt = Math.floor( Math.sqrt( floor ) );
var x = 2;
for ( x; x <= floorSqrt; x++ ){ if ( floor % x == 0 ) return false; }
return true;
},
getPrimes : function ( max, min ){
var primes = [ ];
if ( isNaN(max) ){ return primes; }
var range = NaturalNumber.setRange( min, max, 2 );
var min = range.min;
var max = range.max;
if ( max < 2 ){ return primes; }
if ( min < 2 ){ min = 2; }
for ( min; min <= max; min++ ){
if ( primes.length === NaturalNumber.limit ){ return primes; }
if ( NaturalNumber.isPrime( min ) ){ primes.push( min ); }
}
return primes;
},
choosePrimes : function ( array ){
var primes = [ ], n = 0, value;
for ( n; n < array.length; n++ ){
value = array[ n ];
if ( NaturalNumber.isPrime( value ) ){ primes.push( value ); }
}
return primes;
},
isComposite : function ( number ){
var floor = Math.floor( number );
if ( isNaN(floor) || floor == Infinity || floor != number || floor < 2 ){
return false;
}
var floorSqrt = Math.floor( Math.sqrt( floor ) );
var x = 2;
for ( x; x <= floorSqrt; x++ ){ if ( floor % x == 0 ) return true; }
return false;
},
getComposites : function ( max, min ){
var composites = [ ];
if ( isNaN(max) ){ return composites; }
var range = NaturalNumber.setRange( min, max, 4 );
var min = range.min;
var max = range.max;
if ( max < 4 ){ return composites; }
if ( min < 4 ){ min = 4; }
for ( min; min <= max; min++ ){
if ( composites.length === NaturalNumber.limit ){ return composites; }
if ( NaturalNumber.isComposite( min ) ){ composites.push( min ); }
}
return composites;
},
chooseComposites : function ( array ){
var composites = [ ], n = 0, value;
for ( n; n < array.length; n++ ){
value = array[ n ];
if ( NaturalNumber.isComposite( value ) ){ composites.push( value ); }
}
return composites;
}
};
</script>
소수 관련 사이트 (영문) :
☞ List of prime numbers up to 1,000,000,000,000 (Prime I.T.)
☞ Prime Number Lists (Math is Fun)
☞ The First 1,000 Primes (The Prime Pages)
이 내용이 도움이 되셨다면, 아래의 하트 버튼을 눌러주세요. *^^*
If this article is helpful to you, please click the heart button below. *^^*
'JAVASCRIPT > Number & Math' 카테고리의 다른 글
[ 자바스크립트 ] 0, +0, -0 (0) | 2018.06.25 |
---|---|
[ 자바스크립트 ] 숫자를 문자로 바꾸기 (0) | 2017.07.01 |
[ 자바스크립트 ] Number.isInteger() (0) | 2016.10.26 |
[ 자바스크립트 ] 랜덤으로 실수를 가져오기 (0) | 2016.10.26 |
[ 자바스크립트 ] Math.trunc() (0) | 2016.10.22 |