Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Lecture-011 - Maths for DSA - 1/code/MathsForDSA.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
// decimalno,base
// power=0 ans=0
// while(decimal_no>0){
// int rem=decimal_no%base;
// decimal_no=decimal_no/base;
// ans+=rem*Math.pow(10,power);
// power++;
// }
// }


// a,b
// while(b>0){
// if(b%2!=0){
// res=res*a;
// }
// b=b/2;
// a=a*a;
// }
// }

// 2,5
// res=2
// b=2
// a=4
// b=1
// a=16
// res=32
// a=16*16
// final-------------------res=32

public class MathsForDSA {
public static void main(String[] args) {
decimalToAnyBase(5,2);
Expand Down
56 changes: 39 additions & 17 deletions Lecture-012 - Maths for DSA - 2/code/MathsForDSA2.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ public static void countDigitsViaLog(int num){
int count = (int)Math.log10(num) + 1;
System.out.println("Digits are :"+ count);
}
public static void armstrongNumber(int num){
int res = 0;
int copy = num;
while (num>0){
int digit = num%10;
res = res + (int)Math.pow(digit,3);
public static void armstrongNumber(int num) {
int originalNum = num;
int numberOfDigits = Integer.toString(num).length();
int sum = 0;

// Calculate the sum of each digit raised to the power of the number of digits
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, numberOfDigits);
num /= 10;
}
System.out.println(res==copy);


// Check if the sum is equal to the original number
System.out.println(sum == originalNum);
}
public static void printAllDivisors(int num){
int counter = 1;
Expand All @@ -76,23 +80,41 @@ public static void primeNumbers(int num){
}
System.out.println(num + " is a prime number ");
}
public static void sieveAlgorithm(int num){
boolean arr[] = new boolean[num+1];
import java.util.Arrays;

public class SieveOfEratosthenes {

public static void sieveAlgorithm(int num) {
if (num < 2) {
System.out.println("There are no primes less than 2.");
return;
}

boolean[] arr = new boolean[num + 1];
Arrays.fill(arr, true);
int counter = 2;
while (counter<=num){
if(arr[counter] == true){
for(int factor = counter+counter; factor<=num; factor+=counter){
arr[0] = arr[1] = false; // 0 and 1 are not prime numbers

int limit = (int) Math.sqrt(num);
for (int counter = 2; counter <= limit; counter++) {
if (arr[counter]) {
for (int factor = counter * counter; factor <= num; factor += counter) {
arr[factor] = false;
}
}
counter++;
}

for(int i=2;i<=num;i++){
System.out.println(i +" : "+ arr[i]);
for (int i = 2; i <= num; i++) {
if (arr[i]) {
System.out.println(i + " : " + arr[i]);
}
}
}

public static void main(String[] args) {
sieveAlgorithm(30);
}
}

public static double newtonRaphsonSqRoot(int num){
double tol = 0.0001;
double root;
Expand Down