All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ds.math.MathUtil Maven / Gradle / Ivy

package com.ds.math;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * class for Maths utility class
 * @author piyush
 * @since 0.1
 */
public class MathUtil {
    /**
     * 

This is a method implenenting the no of digit of a number * Superman! *

* @param n * @return int * @see HERO-402 * @since 1.0 */ public int noOfDigits(int n){ /* implementation for iterative approach */ // int count=0; // while(n!=0){ // n=n/10; // count++; // } // return count; /* recursive implementation */ // if(n==0){ // return 0; // } // else{ // return 1+noOfDigits(n/10); // } //best implenentation O(1) return (int)Math.floor(Math.log10(n)+1); } public static void main(String[] args) { MathUtil mathUtil=new MathUtil(); // List arrayList=mathUtil.seiveOfEratosthenes(50); // for (int a:arrayList) { // System.out.println(a); // } // System.out.println(mathUtil.exactly3Divisors(999999)); int n = 10; System.out.println(exactlyThreeDivisors(999999)); } /** *

This is a method return s the list of prime Number upto a number * Superman! *

* @param num * @return int * @see HERO-402 * @since 1.0 */ public List primeNumberUptoN(int num){ int[] arr=new int[num+1]; for (int i=0;i arrayList=new ArrayList(); for (int i = 2; i This is a method return s the list of prime Number upto a number * Superman! *

* @param num * @return int * @see HERO-402 * @since 1.0 */ public List seiveOfEratosthenes(int num){ ArrayList primeNumbers=new ArrayList(); //declare the size of arr as num+1 int[] arr=new int[num+1]; for(int i=0;i primeNumbers=seiveOfEratosthenes(N); System.out.println(primeNumbers.size()); // for(int a:primeNumbers){ // System.out.println(a); // } int count=0; for(int a:primeNumbers){ if((a*a)<=N){ count++; } } return count; } private static int exactlyThreeDivisors(int num) { boolean[] primeSieve = generatePrimeUptoN(num); int j = 2; int count = 0; while ((j * j) <= num) { if (primeSieve[j] == true) { count++; } j++; } return count; } private static boolean[] generatePrimeUptoN(int num) { boolean[] primeSieve = new boolean[num + 1]; Arrays.fill(primeSieve, true); primeSieve[0] = false; primeSieve[1] = false; int range = (int) Math.floor(Math.sqrt(num)); for (int i = 2; i < range; i++) { for (int j = (i * i); j <= num; j = j + i) { if (!primeSieve[j]) { primeSieve[j] = false; } } } return primeSieve; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy