Code Optimization Technique (logical AND and logical OR)
Last Updated :
01 Nov, 2023
Logical AND (&&)
While using && (logical AND), we must put the condition first whose probability of getting false is high so that compiler doesn’t need to check the second condition if the first condition is false.
C++14
#include <bits/stdc++.h>
using namespace std;
// Function to check whether n is odd
bool isOdd(int n) { return (n & 1); }
// Function to check whether n is prime
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); i++)
if ((n % i) == 0)
return false;
return true;
}
int main()
{
int cnt = 0, n = 10;
// Implementation 1
for (int i = 2; i <= n; i++) {
if (isOdd(i) && isPrime(i))
cnt++;
}
cnt = 0;
n = 10;
// Implementation 2
for (int i = 2; i <= n; i++) {
if (isPrime(i) && isOdd(i))
cnt++;
}
}
Java
public class Main {
// Function to check whether n is odd
public static boolean isOdd(int n) {
return n % 2 != 0;
}
// Function to check whether n is prime
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int cnt = 0;
int n = 10;
// Implementation 1
for (int i = 2; i < n; i++) {
if (isOdd(i) && isPrime(i)) {
cnt++;
}
}
System.out.println("Implementation 1: " + cnt);
cnt = 0;
n = 10;
// Implementation 2
for (int i = 2; i < n; i++) {
if (isPrime(i) && isOdd(i)) {
cnt++;
}
}
System.out.println("Implementation 2: " + cnt);
}
}
Python3
# Function to check whether n is odd
def isOdd(n):
pass
# Function to check whether n is prime
def isPrime(n):
pass
if __name__ == '__main__':
cnt = 0; n = 10
# Implementation 1
for i in range(2,n) :
if isOdd(i) and isPrime(i):
cnt+=1
cnt = 0
n = 10
# Implementation 2
for i in range(2,n):
if isPrime(i) and isOdd(i):
cnt+=1
C#
using System;
class MainClass {
// Function to check whether n is odd
public static bool isOdd(int n) {
return n % 2 != 0;
}
// Function to check whether n is prime
public static bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.Sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void Main() {
int cnt = 0;
int n = 10;
// Implementation 1
for (int i = 2; i < n; i++) {
if (isOdd(i) && isPrime(i)) {
cnt++;
}
}
Console.WriteLine("Implementation 1: " + cnt);
cnt = 0;
n = 10;
// Implementation 2
for (int i = 2; i < n; i++) {
if (isPrime(i) && isOdd(i)) {
cnt++;
}
}
Console.WriteLine("Implementation 2: " + cnt);
}
}
JavaScript
<script>
// Function to check whether n is odd
function isOdd(n) { return (n & 1); }
// Function to check whether n is prime
function isPrime(n)
{
if (n <= 1)
return false;
for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++)
if ((n % i) == 0)
return false;
return true;
}
let cnt = 0, n = 10;
// Implementation 1
for (let i = 2; i <= n; i++) {
if (isOdd(i) && isPrime(i))
cnt++;
}
cnt = 0;
n = 10;
// Implementation 2
for (let i = 2; i <= n; i++) {
if (isPrime(i) && isOdd(i))
cnt++;
}
// This code is contributed by Pushpesh Raj.
</script>
Consider the above implementation:
In implementation 1, we avoid checking even numbers whether they are prime or not as primality test requires more computation than checking a number for even/odd.
Probability of a number getting odd is more than of it being a prime that's why we first check whether the number is odd before checking it for prime.
On the other hand in implementation 2, we are checking whether the number is prime or not before checking whether it is odd which makes unnecessary computation as all even numbers other than 2 are not prime but the implementation still checks them for prime.
Logical OR (||)
While using || (logical OR), we must put the condition first whose probability of getting true is high so that compiler doesn’t need to check the second condition if the first condition is true.
C++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to check whether n is odd
bool isEven(int n) {
return (n % 2 == 0);
}
// Function to check whether n is prime
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int cnt = 0, n = 10;
// Implementation 1
for (int i = 3; i <= n; i++) {
if (isEven(i) || !isPrime(i))
cnt++;
}
return 0;
}
// This code is contributed by codebraxnzt
C
#include <stdio.h>
// Function to check whether n is odd
bool isEven(int n);
// Function to check whether n is prime
bool isPrime(int n);
int main()
{
int cnt = 0, n = 10;
// Implementation 1
for (int i = 3; i <= n; i++) {
if (isEven(i) || !isPrime(i))
cnt++;
}
}
Java
import java.io.*;
class GFG {
// Function to check whether n is odd
boolean isEven(int n);
// Function to check whether n is prime
boolean isPrime(int n);
public static void main (String[] args)
{
int cnt = 0, n = 10;
// Implementation 1
for (int i = 3; i <= n; i++) {
if (isEven(i) || !isPrime(i))
cnt++;
}
}
}
// This code is contributed by Utkarsh
Python3
# Function to check whether n is even
def isEven(n):
pass
# Function to check whether n is prime
def isPrime(n):
pass
if __name__ == '__main__':
cnt = 0; n = 10
# Implementation 1
for i in range(3,n) :
if isOdd(i) or not isPrime(i):
cnt+=1
C#
using System;
class GFG {
// Function to check whether n is odd
bool isEven(int n)
{
return (n % 2 == 0);
}
// Function to check whether n is prime
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i <= Math.Sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
static void Main(string[] args)
{
int cnt = 0, n = 10;
// Implementation 1
for (int i = 3; i <= n; i++) {
if (new GFG().isEven(i) || !new GFG().isPrime(i))
cnt++;
}
}
}
JavaScript
// Function to check whether n is odd
function isEven(n);
// Function to check whether n is prime
function isPrime(n);
let cnt = 0, n = 10;
// Implementation 1
for (let i = 3; i <= n; i++) {
if (isEven(i) || !isPrime(i))
cnt++;
}
// This code is contributed by Aman Kumar
As described earlier that the probability of a number being even is more than that of it being a non-prime. The current order of execution of the statements doesn't allow even numbers greater than 2 to be checked whether they are non-prime (as they are all non-primes).
Note: For larger inputs, the order of the execution of statements can affect the overall execution time for the program.
Similar Reads
What are the differences between bitwise and logical AND operators in C/C++? A Bitwise And operator is represented as '&' and a logical operator is represented as '&&'. The following are some basic differences between the two operators. a) The logical and operator '&&' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean va
4 min read
Short Circuit Logical Operators in Java with Examples In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit ev
3 min read
Propositional Logic Notes for GATE Exam In the context of the GATE (Graduate Aptitude Test in Engineering) examination, Discrete Mathematics plays a crucial role) Topics in this domain include logic, set theory, combinatorics, graph theory, and relations. These Notes focus on the Propositional and first order logic of the discrete mathema
9 min read
What is Boolean Expression In this article, we will see what is Boolean Expression. Before starting with the topic directly lets us see what is a Boolean expression. It is an expression that always yields two values either true or false when evaluated. If the condition is true then it will return true or false and vice versa.
4 min read
PHP | Bitwise Operators The Bitwise operators is used to perform bit-level operations on the operands. The operators are first converted to bit-level and then calculation is performed on the operands. The mathematical operations such as addition , subtraction , multiplication etc. can be performed at bit-level for faster p
5 min read
Logical Operators in Solidity Logical Operators are used to combining two or more conditions. Solidity has the following types of logical operators: Logical AND: Logical AND takes two operands and gives the valid Boolean result. The logical AND operator evaluates to true when all the operands are true (non-zero) otherwise false(
2 min read