The assignment was to write a program which reads in an integer k, and prints out the number of positive integers between 1 and 100000 (inclusive) which have exactly k divisors. As an example, the number 24 has 8 divisors: 1, 2, 3, 4, 6, 8, 12, and 24.
I have a running programme, but is there anyway i could make the search faster??
#include <stdio.h>
#include <math.h>
int main(void)
{ int a; //user input//
int divisors; //running total of number of divisors//
int sum; //running total of numbers with the required number of divisors//
printf("Enter the target number of divisors:");
scanf("%d", &a);
printf("\n");
int i;
for (i=1; i<=100000; i++)
{
divisors=2;
int p;
for(p=2; p<i; p++)
{if (i%p==0)
divisors++;}
if (divisors==a)
sum++;}
printf("There are %d numbers between 1 and 100000 inclusive which have exactly %d divisors.", sum, a);
return 0;
}