Created
July 16, 2019 04:20
-
-
Save sogwiz/512bae4a0b760506ca6bc171cfe01ec8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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