MATHEMATICAL PROGRAMS
MATHEMATICAL PROGRAMS
GEEKSFORGEEKS:
class Solution {
public static int seriesSum(int n) {
// code here
int sum=0;
for(int i=1;i<=n;i++){
sum=sum+i;
}
return sum;
}
}
class Solution {
public int nthFibonacci(int n) {
// code here
if(n==0){
return 0;
}
if(n==1){
return 1;
}
int a=0;
int b=1;
for(int i=2;i<=n;i++){
int next=a+b;
a=b;
b=next;
}
return b;
}
}
FIBONACCI SUM:
class Solution {
static final long MOD = 1000000007;
static long fibSum(long N) {
// code here
if(N==0){
return 0;
}
if(N==1){
return 1;
}
long a=0,b=1;
long sum=a+b;
for(int i=2;i<=N;i++){
long next=(a+b) % MOD;
sum=(sum+next) % MOD;
a=b;
b=next;
}
return sum;
}
}
PRIME NUMBER:
class Solution {
static boolean isPrime(int n) {
// code here
int count=0;
if(n<=1){
return false;
}
for(int i=2;i*i<=n;i++){
if(n%i==0){
return false;
}
}
return true;
}
}
class Solution {
public static int findUnion(int a[], int b[]) {
// code here
HashSet<Integer> result=new HashSet<>();
for(int i=0;i<a.length;i++){
result.add(a[i]);
}
for(int j=0;j<b.length;j++){
result.add(b[j]);
}
return result.size();
}
}
SQUARE ROOT:
class Solution {
int floorSqrt(int n) {
// Your code here
int result=(int) Math.sqrt(n);
return result;
}
}
class Solution {
public static long[] productExceptSelf(int arr[]) {
// code here
int n=arr.length;
long suffix=1;
long [] prefix=new long[n];
long [] result=new long[n];
prefix[0]=1;
for(int i=1;i<n;i++){
prefix[i]=prefix[i-1]*arr[i-1];
}
for(int i=n-1;i>=0;i--){
result[i]=prefix[i]*suffix;
suffix=suffix*arr[i];
}
return result;
}
}
N TH FIBONACCI SERIES:
class Solution
{
//Function to return list containing first n fibonacci numbers.
public static long[] printFibb(int n)
{
//Your code here
if(n==1){
return new long[] {1};
}
long [] result=new long[n];
result[0]=1;
result[1]=1;
for(int i=2;i<n;i++){
result[i]=result[i-1]+result[i-2];
}
return result;
}
}
class Solution {
public static int numberofElementsInIntersection(int a[], int b[]) {
// Your code here
int count=0;
for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
if(a[i]==b[j]){
count=count+1;
}
}
}
return count;
}
}
class Solution {
public static int numberofElementsInIntersection(int a[], int b[]) {
// Your code here
HashSet<Integer> set=new HashSet<>();
for(int i=0;i<a.length;i++){
set.add(a[i]);
}
for(int j=0;j<b.length;j++){
set.add(b[j]);
}
int len=a.length+b.length;
return len-set.size();
}
}
class Solution {
// Function to return the position of the first repeating element.
public static int firstRepeated(int[] arr) {
// Your code here
HashMap<Integer,Integer> map=new HashMap<>();
for(int num:arr){
map.put(num, map.getOrDefault(num,0) + 1);
}
for(int i=0;i<arr.length;i++){
int num=arr[i];
if(map.get(num)>=2){
return i+1;
}
}
return -1;
}
}
PERFECT NUMBERS:
class Solution {
static boolean isPerfectNumber(int n) {
// code here
ArrayList<Integer> result=new ArrayList<>();
for(int i=1;i<n;i++){
if(n%i==0){
result.add(i);
}
}
int sum=0;
for(int i=0;i<result.size();i++){
sum=sum+result.get(i);
}
return sum==n;
}
};
EFFICIENT CODE:
class Solution {
static boolean isPerfectNumber(int n) {
// code here
int sum=1;
if(n<=1){
return false;
}
for(int i=2;i<Math.sqrt(n);i++){
if(n%i==0){
sum+=i;
sum+=n/i;
}
}
return sum==n;
}
};
(OR)
class Solution {
static boolean isPerfectNumber(int n) {
// code here
int sum=1;
if(n<=1){
return false;
}
for(int i=2;i<Math.sqrt(n);i++){
if(n%i==0){
sum+=i;
if(i!=n/i){
sum+=n/i;
}
}
}
return sum==n;
}
};
class Solution {
public static int sumOfDivisors(int n) {
// code here
int sum=1;
for(int i=2;i<=n;i++){
for(int j=1;j<=i;j++){
if(i%j==0){
sum+=j;
}
}
}
return sum;
}
}
ARITHMETIC SEQUENCE:
class Solution {
static int inSequence(int a, int b, int c) {
// code here
if((b-a)%c==0 && (b-a)/c>=0){
return 1;
}
return 0;
}
}
ARMSTRONG NUMBER:
NOTE: "HERE important is to keep original as n to make a other copy.. because n
varies as it used in the loop"..
class Solution {
static boolean armstrongNumber(int n) {
// code here
int sum=0;
while(n>=0){
int rem=n%10;
rem=(int)Math.pow(rem,3);
sum+=rem;
n=n/10;
}
return sum==n;
}