Saturday 18 April 2020

How to check if a number is prime?

How to check if a number is prime?


Question: What is Prime Number
A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.


Question: Give the example of Prime Number
2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.


Question: Write an function that check the prime number OR Not?
function checkPrime($number){
    if($number < 0 ){
        return false;
    }
    $return=true;
    for($i=2; $i < $number; $i++){
        if($number % $i ==0){
            $return=false;
            break;
        }
    }
   return $return;
}



Question: How to check a number is prime Or Not?
echo checkPrime(2)?'Prime':'Not Prime'; //Prime
echo checkPrime(4)?'Prime':'Not Prime'; //Not Prime
echo checkPrime(6)?'Prime':'Not Prime'; //Not prime
echo checkPrime(7)?'Prime':'Not Prime';  //Prime


Question: What are the prime numbers between 1 to 100?
for( $i = 2; $i < 100; $i++ ){
    if( checkPrime( $i ) ){
        echo $i;
        echo ',';
    }
}






Wednesday 8 April 2020

Difference between mysql.createConnection and mysql.createPool in Node.js?

Difference between mysql.createConnection and mysql.createPool in Node.js?

mysql.createConnection

When you create a connection with mysql.createConnection, you only have one connection and it lasts until you close it OR connection closed by MySQL.

A single connection is blocking. While executing one query, it cannot execute others. hence, your application will not perform good.
Example:
var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'mydb',
});


mysql.createPool

mysql.createPool is a place where connections get stored.
When you request a connection from a pool,you will receive a connection that is not currently being used, or a new connection.
If you're already at the connection limit, it will wait until a connection is available before it continues.

A pool while one connection is busy running a query, others can be used to execute subsequent queries. Hence, your application will perform good.
Example:
var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'mydb',
});