[英]given a number n , we have to find out such numbers that are less than or equal to n, that have exactly 3 divisors
我已經使用過擦除數篩法來計算具有正好3個除數並且小於或等於n的數字,其中n是給定的數字。
此代碼的問題是它給出n = 999999的答案37386,但正確的答案是168
public static void main (String[] args) throws java.lang.Exception
{
Scanner scn =new Scanner(System .in);
print("enter the prime no");
int n= scn.nextint();
int arr[]= new int[n+1];
for(int i=2;i<arr.length;i++)
{
arr[i]=1;
}
for(int i=2;i*i<=n;i++)
{
if(arr[i]==1)
{
for(int p=2;p*i<=n;p++)
{
arr[p*i]=0;
}
}
}
int count=0;
for(int i=2;i<=n;i++)
{
if(arr[i]==1&&i*i<n)
{
count++;
}
}
System.out.print(count);
}
Eratosthenes的篩子用於計算素數。 您的代碼正確地計算出有少於379999個質數的37386個質數。應該可以修改該代碼以找到正好具有三個除數而不是一個除數的數字,但是我將其作為練習者進行詢問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.