Introduction
Math coding problems often require us to use various math properties and techniques to find patterns and solve them. By applying these properties, we can simplify complex problems and come up with efficient solutions. In this blog post, we will explore some common math properties that can be used to solve coding problems.
1. Arithmetic Math Operations
Arithmetic operations are the foundation of math coding problems. These operations include addition, subtraction, multiplication, and division. By using these operations, we can perform calculations and manipulate numbers to solve problems.
For example, let’s say we have a coding problem that requires us to find the sum of all numbers in an array. We can use the addition operation to iterate through the array and keep adding each element to a running total.
In coding, we should never perform these operations over double or flotant data types because there is no precision, but we have to know that if we use integers, we may lose the decimal part unless we save the fraction, also the math order of operations is respected.
#include<bits/stdc++.h>
using namespace std;
int main(){
long long result;
result=0;
result=((10+8)/2)*6-3;
cout<<result<<endl;
return 0;
}
2. Modulo Operator
The modulo operator, denoted by “%”, returns the remainder of a division operation. This operator is particularly useful in coding problems that involve patterns and repetitions.
For instance, consider a problem where we need to determine if a number is even or odd. We can use the modulo operator to divide the number by 2. If the remainder is 0, the number is even; otherwise, it is odd.
To execute the arithmetic operation over a specific base, we will need the modular arithmetic operations, which are defined by the next rules:
- Addition: Add the two values, then get the modulus again (a+b)%m
- Subtraction: Subtract the value and add the modulus, then get the modulus again (a-b+m)%m
- Multiplication: Get the modulus of the two numbers and multiply them, then get the modulus again (a%m * b%m)%m
#include<bits/stdc++.h>
using namespace std;
int main(){
long long result=0;
long long base=8;
//Addition (a+b)%m
cout<<(5+10)%8<<endl;
//Subtraction (a-b+m)%m
cout<<(3-5+base)%base<<endl;
//Multiplication (a%m * b%m)%m
cout<<(5%base * 9%base)%base<<endl;
return 0;
}
3. Prime Numbers
Prime numbers are integers greater than 1 that have no divisors other than 1 and themselves. They play a significant role in coding problems that involve factors, divisors, or finding patterns in numbers.
For example, let’s say we have a problem that requires us to find the largest prime factor of a given number. We can use the concept of prime numbers to solve this problem efficiently. By iterating from the largest possible factor downwards, we can check if each number is a factor and if it is prime. Once we find a prime factor, we can stop the iteration and return the result.
Get all prime numbers until a specific number using the Eratosthenes sieve
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lli;
lli maxi=11;//define until which number to calculate primes
vector<bool>isprime(maxi,1);
vector<lli>primes;
void sieve(){
isprime[0]=isprime[1]=0;
for(int i=2;i*i<=maxi;i++){
if(isprime[i]==0)continue;
for(int j=i*i;j<maxi;j+=i){
isprime[j]=0;
}
}
for(int i=2;i<=maxi;i++){
if(isprime[i]==0)continue;
primes.push_back(i);
}
}
int main(){
sieve();
for(auto n:primes){
cout<<n<<" ";
}
//Stdout: 2 3 5 7 11
return 0;
}
4. Fibonacci Sequence
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. This sequence is widely used in coding problems that involve patterns, sequences, or dynamic programming.
For instance, let’s consider a problem where we need to find the nth number in the Fibonacci sequence. We can use a loop or recursion to generate the sequence up to the desired number and return the result.
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lli;
int main(){
lli maxi=10;
vector<lli>fibo(maxi);
fibo[0]=fibo[1]=1;
for(lli fn=2;fn<maxi;fn++){
fibo[fn]=fibo[fn-1]+fibo[fn-2];
}
for(lli fn=0;fn<maxi;fn++){
cout<<fibo[fn]<<" ";
}
//Stdout: 1 1 2 3 5 8 13 21 34 55
return 0;
}
Math Conclusion
Math coding problems can be effectively solved by utilizing various math properties and techniques. From basic arithmetic operations to more advanced concepts like prime numbers and the Fibonacci sequence, these properties enable us to find patterns, simplify problems, and derive efficient solutions. By understanding and applying these properties, we can enhance our problem-solving skills and tackle a wide range of math coding problems.
I think math is importante for other areas, not also in competitive programming, these topics can be useful there too