Check Power of 2 Using Bitwise Operations in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:47:20

406 Views

We are required to write a JavaScript function that takes in a number and determines whether or not it is a power of two.For example −f(23) = false f(16) = true f(1) = true f(1024) = trueApproach −Powers of two in binary form always have just one bit. Like this −1: 0001 2: 0010 4: 0100 8: 1000Therefore, after checking that the number is greater than zero, we can use a bitwise hack to test that one and only one bit is set. The same is shown below −num & (num - 1)ExampleFollowing is the code −const num1 = 256; ... Read More

Euclidean Algorithm for Calculating GCD in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:46:15

2K+ Views

In mathematics, Euclid's algorithm, is a method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder.The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number.For example, 21 is the GCD of 252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 252 − 105 = 147.Since this replacement ... Read More

Primality Test of Numbers in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:44:50

487 Views

A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. All other natural numbers greater than 1 are called composite numbers. A primality test is an algorithm for determining whether an input number is prime.We are required to write a JavaScript function that takes in a number and checks whether it is a prime or not.ExampleFollowing is the code −const findPrime = (num = 2) => {    if (num % 1 !== 0) {       return false;    }    if (num

Check Due Date and Current Date Records in MySQL WHERE Clause

AmitDiwan
Updated on 11-Dec-2020 05:57:50

700 Views

To check for such conditions, use IF() in MySQL.Let us create a table −Examplemysql> create table demo89    -> (    -> duedate date    -> ); Query OK, 0 rows affected (0.78Insert some records into the table with the help of insert command −Examplemysql> insert into demo89 values('2020-01-10'); Query OK, 1 row affected (0.55 mysql> insert into demo89 values(null); Query OK, 1 row affected (0.13 mysql> insert into demo89 values('2020-11-29'); Query OK, 1 row affected (0.15 mysql> insert into demo89 values('2019-11-29'); Query OK, 1 row affected (0.09Display records from the table using select statement −Examplemysql> select ... Read More

Creating a Table with MySQL Hibernate

AmitDiwan
Updated on 11-Dec-2020 05:55:53

2K+ Views

To create a table, you need to insert below line into application.properties −spring.jpa.hibernate.ddl-auto=updateHere, Hibernate will create the table demo88 automatically. The application.properties code is as follows −spring.datasource.platform=mysql spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update server.port=8191 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/sampledatabase spring.datasource.username=root spring.datasource.password=123456The demo88 entity class is as follows to create table columns −Examplepackage com.automaticallytablecreation; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table public class demo88 {    @Id    private int id;    @Column(name="name")    private String name; }The main class code is as follows −Examplepackage com.automaticallytablecreation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AutomaticTableApplication {    public static void main(String[] ... Read More

Display MySQL Table Values Using Java

AmitDiwan
Updated on 11-Dec-2020 05:50:52

5K+ Views

For this, you can use the ResultSet concept. For connection, we will be using the MySQL JDBC Driver.Let us create a table −Examplemysql> create table demo87    -> (    -> name varchar(20),    -> age int    -> )    -> ; Query OK, 0 rows affected (0.62Insert some records into the table with the help of insert command −Examplemysql> insert into demo87 values('John', 21); Query OK, 1 row affected (0.15 mysql> insert into demo87 values('David', 23); Query OK, 1 row affected (0.12 mysql> insert into demo87 values('Bob', 22); Query OK, 1 row affected (0.16Display records from ... Read More

Check If Any Value is Null in a MySQL Table Single Row

AmitDiwan
Updated on 11-Dec-2020 05:47:37

633 Views

For this, you can use ISNULL in MySQL.Let us create a table −Examplemysql> create table demo86    -> (    -> value1 varchar(20)    -> ,    -> value2 varchar(20)    -> ); Query OK, 0 rows affected (2.77Insert some records into the table with the help of insert command −Examplemysql> insert into demo86 values(null, null); Query OK, 1 row affected (0.34 mysql> insert into demo86 values(null, 'John'); Query OK, 1 row affected (0.16 mysql> insert into demo86 values('David', 'Mike'); Query OK, 1 row affected (0.17 mysql> insert into demo86 values('Sam', null); Query OK, 1 row affected ... Read More

MySQL Sum Rows with Same ID

AmitDiwan
Updated on 11-Dec-2020 05:44:45

8K+ Views

To sum rows with same ID, use the GROUP BY HAVING clause.Let us create a table −Examplemysql> create table demo84    -> (    -> id int,    -> price int    -> )    -> ; Query OK, 0 rows affected (0.60Insert some records into the table with the help of insert command −Examplemysql> insert into demo84 values(1, 2000); Query OK, 1 row affected (0.08 mysql> insert into demo84 values(1, 2000); Query OK, 1 row affected (0.14 mysql> insert into demo84 values(2, 1800); Query OK, 1 row affected (0.14 mysql> insert into demo84 values(2, 2200); Query ... Read More

MySQL Query for INSERT INTO Using Values from Another Table

AmitDiwan
Updated on 11-Dec-2020 05:42:20

952 Views

For this, use INSERT INTO SELECT statement.Let us create a table −Examplemysql> create table demo82    -> (    -> id int,    -> name varchar(20)    -> ); Query OK, 0 rows affected (2.06Insert some records into the table with the help of insert command −Examplemysql> insert into demo82 values(100, 'John'); Query OK, 1 row affected (0.14 mysql> insert into demo82 values(101, 'Bob'); Query OK, 1 row affected (0.32 mysql> insert into demo82 values(101, 'David'); Query OK, 1 row affected (0.09 mysql> insert into demo82 values(101, 'Mike'); Query OK, 1 row affected (0.12 mysql> insert ... Read More

MySQL SELECT WHERE Values in List String

AmitDiwan
Updated on 11-Dec-2020 05:39:59

4K+ Views

For this, use FIND_IN_SET().Let us create a table −Examplemysql> create table demo81    -> (    -> id int not null auto_increment primary key,    -> username varchar(200)    -> ); Query OK, 0 rows affected (1.44Insert some records into the table with the help of insert command −Examplemysql> insert into demo81(username) values('John, Chris, David'); Query OK, 1 row affected (0.11 mysql> insert into demo81(username) values('Mike, Sam'); Query OK, 1 row affected (0.14 mysql> insert into demo81(username) values('Chris, Bob, Sam'); Query OK, 1 row affected (0.13 mysql> insert into demo81(username) values('Mike, John, Chris'); Query OK, 1 row ... Read More

Advertisements