2014-09-09 6 views
-1

이것은 C로 작성된 간단한 프로그램으로 사용자에게 int n을 묻습니다.
2에서 n까지 두 개의 연속 소수 중에서 가장 높은 차이를 찾는 가장 좋은 방법은 무엇입니까?두 연속 소수 간의 가장 큰 차이

int main() { 
    int n, i, j, c; 
    clrscr(); 
    printf("Enter Range To Print Prime Numbers:"); 
    scanf("%d", &n); 
    printf("Prime Numbers Are Following:\n"); 
    for(i=1, c=0; i<=n; i++) { 
     for (j=1; j<=i; j++) 
      if (i % j == 0) 
       c++; 
     if (c == 2) 
      printf("%d ", i); 
    } 

    printf("\n\n The highest difference between numbers is:"); 

    /* logic i cant get */ 
    getch(); 
} 
+1

첫 번째와 마지막 차이가 가장 큽니다. – haccks

+1

@ haccks "연속 된"단어가 누락 된 것 같습니다. – dasblinkenlight

+0

@ dasblinkenlight; 나도 알아 : – haccks

답변

1

변수의 두 연속 소수 사이의 최대 차이를 추적하고 새 차이가 더 큰 경우 변수를 새 차이로 바꾸십시오.

int maxDifference = 0; 
int currentDifference; 

// begin looping 
currentDifference = calculateNewDifference(); 

if(currentDifference > maxDifference) { 
    maxDifference = currentDifference; 
} 
+0

newDifference ?? – tabia

+0

죄송합니다. 이것은 의사 코드입니다. 두 개의 최신 소수 사이의 차이입니다. – AGreenman

+0

그가 논리에 어려움을 겪고 있다면 효율성이 가장 큰 관심사라고 생각하지 않습니다. – AGreenman