0% found this document useful (0 votes)
2 views

MATHEMATICAL PROGRAMS

The document contains various mathematical programming problems and their solutions in Java, including calculating the sum of natural numbers, finding the Nth Fibonacci number, checking for prime numbers, and determining perfect numbers. Each problem is accompanied by a class Solution with a specific method to implement the solution. The document serves as a resource for basic and easy-level mathematical programming challenges.

Uploaded by

thirishudoll
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MATHEMATICAL PROGRAMS

The document contains various mathematical programming problems and their solutions in Java, including calculating the sum of natural numbers, finding the Nth Fibonacci number, checking for prime numbers, and determining perfect numbers. Each problem is accompanied by a class Solution with a specific method to implement the solution. The document serves as a resource for basic and easy-level mathematical programming challenges.

Uploaded by

thirishudoll
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

MATHEMATICAL PROGRAMS:

BASIC AND EASY LEVEL:

GEEKSFORGEEKS:

SUM OF N NATURAL NUMBERS:

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;
}
}

NTH FIBONACCI NUMBER:

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;
}
}

UNION OF ARRAY WITH DUPLICATES:

.size() operator used in hashset to return its count of integers in it.

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;
}
}

PRODUCT ARRAY PUZZLE:

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;
}
}

INTERSECTION OF ARRAYS WITH DISTINCT:

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;
}
}

EFFICIENT WAY:(USING HASHSET)

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();
}
}

FIRST REPEATING ELEMENT:

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;
}
};

SUM OF ALL DIVISORS FROM 1 TO 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;
}
}

formula: b= a + (n-1) * c; (to find the next term)


but to find the term exits in the series?
n=((b-a)/c)+1;
that is (b-a)%c ==0
(b-a)/c should not be a fraction want to be a whole number.

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;
}

You might also like