Skip to content

Instantly share code, notes, and snippets.

@sogwiz
Created July 16, 2019 04:20
Show Gist options
  • Save sogwiz/512bae4a0b760506ca6bc171cfe01ec8 to your computer and use it in GitHub Desktop.
Save sogwiz/512bae4a0b760506ca6bc171cfe01ec8 to your computer and use it in GitHub Desktop.
package com.learning.leet.math;
/**
* Created by sargonbenjamin on 7/15/19.
* https://leetcode.com/explore/interview/card/top-interview-questions-easy/102/math/744/
*/
public class CountPrimes {
public int countPrimes(int n) {
if(n<=1)return 0;
int count = 0;
for(int i = 2; i<n; i++){
if(isPrime(i))count++;
}
return count;
}
public boolean isPrime(int val){
for(int i = 2 ; i<=Math.sqrt(val); i++) {
if(val%i == 0)return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment