Efficient way to multiply with 7
Last Updated :
16 Mar, 2023
Given a number n, find the result after multiplying it by 7.
Example:
Input: n=8
Output: 56
Naive Approach: The basic way to solve this is to use a multiplication operator, add the number 7 times or use recursive function.
C++
#include <bits/stdc++.h>
using namespace std;
// c++ implementation
long multiplyBySeven(long n) { return (n * 7); }
/* Driver program to test above function */
int main()
{
long n = 4;
cout << multiplyBySeven(n);
return 0;
}
Java
// Java implementation
import java.io.*;
class GFG {
static long multiplyBySeven(long n) { return (n * 7); }
/* Driver program to test above function */
public static void main (String[] args) {
long n = 4;
System.out.println(multiplyBySeven(n));
}
}
// This code is contributed by Aman Kumar
C#
// C# implementation
using System;
public class GFG{
static long multiplyBySeven(long n) { return (n * 7); }
/* Driver program to test above function */
static public void Main (){
long n = 4;
Console.Write(multiplyBySeven(n));
}
}
// This code is contributed by Pushpesh Raj.
Python3
def multiplyBySeven(n: int) -> int:
return n * 7
n = 4
print(multiplyBySeven(n))
PHP
<?php
function multiplyBySeven($n) {
return $n * 7;
}
$n = 4;
echo multiplyBySeven($n);
?>
JavaScript
<script>
function multiplyBySeven(n) {
return n * 7;
}
let n = 4;
console.log(multiplyBySeven(n));
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Efficient Approach: We can multiply a number by 7 using a bitwise operator.
- First left shift the number by 3 bits (you will get 8n)
- Then subtract the original number from the shifted number
- Return the difference (8n - n).
Below is the implementation of the above approach:
C
# include<stdio.h>
int multiplyBySeven(unsigned int n)
{
/* Note the inner bracket here. This is needed
because precedence of '-' operator is higher
than '<<' */
return ((n<<3) - n);
}
/* Driver program to test above function */
int main()
{
unsigned int n = 4;
printf("%u", multiplyBySeven(n));
getchar();
return 0;
}
CPP
# include<bits/stdc++.h>
using namespace std;
//c++ implementation
long multiplyBySeven(long n)
{
/* Note the inner bracket here. This is needed
because precedence of '-' operator is higher
than '<<' */
return ((n<<3) - n);
}
/* Driver program to test above function */
int main()
{
long n = 4;
cout<<multiplyBySeven(n);
return 0;
}
Java
// Java program to multiply any
// positive number to 7
class GFG {
static int multiplyBySeven(int n)
{
/* Note the inner bracket here.
This is needed because precedence
of '-' operator is higher
than '<<' */
return ((n << 3) - n);
}
// Driver code
public static void main (String arg[])
{
int n = 4;
System.out.println(multiplyBySeven(n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to multiply any
# positive number to 7
# Function to multiply any number with 7
def multiplyBySeven(n):
# Note the inner bracket here.
# This is needed because
# precedence of '-' operator is
# higher than '<<'
return ((n << 3) - n)
# Driver code
n = 4
print(multiplyBySeven(n))
# This code is contributed by Danish Raza
C#
// C# program to multiply any
// positive number to 7
using System;
class GFG
{
static int multiplyBySeven(int n)
{
/* Note the inner bracket here. This is needed
because precedence of '-' operator is higher
than '<<' */
return ((n << 3) - n);
}
// Driver code
public static void Main ()
{
int n = 4;
Console.Write(multiplyBySeven(n));
}
}
// This code is contributed by Sam007
PHP
<?php
function multiplyBySeven($n)
{
// Note the inner bracket here.
// This is needed because
// precedence of '-' operator
// is higherthan '<<'
return (($n<<3) - $n);
}
// Driver Code
$n = 4;
echo multiplyBySeven($n);
// This code is contributed by vt_m.
?>
JavaScript
<script>
function multiplyBySeven(n)
{
/* Note the inner bracket here. This is needed
because precedence of '-' operator is higher
than '<<' */
return ((n << 3) - n);
}
// Driver program to test above function
n = 4;
document.write(multiplyBySeven(n));
// This code is contributed by simranarora5sos
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3:
1. We know that 7 can be represented as 2^3 - 1, which means that 7 can be expressed as (2^3) - 1.
2. So, we can write the multiplication of a number by 7 as:
7*n = (2^3 - 1)*n = (2^3)*n - n = (n << 3) - n
3. Here, << operator is used for left shift operation by 3 bits, which is equivalent to multiplying by 2^3.
We can further simplify the above expression as:
7*n = (n << 3) - n = (n << 2 + n << 1 + n)
Here, << operator is used for left shift operation by 1 bit, which is equivalent to multiplying by 2.
C++
#include <iostream>
using namespace std;
int multiplyBy7(int num) {
return (num << 2) + (num << 1) + num;
}
int main() {
int n = 4;
cout << multiplyBy7(n) << endl; // Output: 28
return 0;
}
Java
public class GFG {
public static int multiplyBy7(int num) {
return (num << 2) + (num << 1) + num;
}
public static void main(String[] args) {
int n = 4;
System.out.println(multiplyBy7(n)); // Output: 28
}
}
Python3
def multiply_by_7(num):
return (num << 2) + (num << 1) + num
#Driver code
n = 4
print(multiply_by_7(n))
C#
using System;
namespace MultiplyBy7 {
class Program {
static int MultiplyBy7(int num) {
return (num << 2) + (num << 1) + num;
}
static void Main(string[] args) {
int n = 4;
Console.WriteLine(MultiplyBy7(n)); // Output: 28
}
}
}
JavaScript
function multiplyBy7(num) {
return (num << 2) + (num << 1) + num;
}
let n = 4;
console.log(multiplyBy7(n)); // Output: 28
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: Works only for positive integers.
Same concept can be used for fast multiplication by 9 or other numbers.
Similar Reads
Multiplication with a power of 2 Given two numbers x and n, we need to multiply x with 2nExamples : Input : x = 25, n = 3 Output : 200 25 multiplied by 2 raised to power 3 is 200. Input : x = 70, n = 2 Output : 280 A simple solution is to compute n-th power of 2 and then multiply with x. C++ // Simple C/C++ program // to compute x
5 min read
Multiply a number with 10 without using multiplication operator Given a number, the task is to multiply it with 10 without using multiplication operator?Examples: Input : n = 50 Output: 500 // multiplication of 50 with 10 is = 500 Input : n = 16 Output: 160 // multiplication of 16 with 10 is = 160 A simple solution for this problem is to run a loop and add n wit
5 min read
Round to next greater multiple of 8 Given an unsigned integer x. Round it up to the next greater multiple of 8 using bitwise operations only.Examples: Input : 35 Output : 40 Input : 64 Output : 64 (As 64 is already a multiple of 8. So, no modification is done.) Solution 1: We first add 7 and get a number x + 7, then we use the techniq
4 min read
Minimum multiplications with {2, 3, 7} to make two numbers equal Given two numbers A and B, the task is to find the minimum number of operations required to make A and B equal. In each operation, any number can be divided by either 2, 3 or 7. If it is not possible then print -1. Examples: Input: A = 14, B = 28 Output: 1 Operation 1: A * 2 = 14 * 2 = 28 which is e
10 min read
Multiply a number by 15 without using * and / operators Given a integer N, the task is to multiply the number with 15 without using multiplication * and division / operators. Examples: Input: N = 10 Output: 150 Input: N = 7 Output: 105 Method 1: We can multiply integer N by 15 using bitwise operators. First left shift the number by 4 bits which is equal
6 min read