Ejemplo n.º 1
0
int main(void)
{
  FindPrimes find_primes(10001);
  list_of_primes primes = find_primes();
  std::cout << "Number of primes: " << primes.size() << std::endl;
  std::cout << "10001st number:   " << primes.back() << std::endl;
}
Ejemplo n.º 2
0
int main(int argc, char** argv) {
    int plen, diff, k, i_l, j_l, k_l;
    int* i_s;
    int* j_s;
    int* k_s;
    int* primes = find_primes(10000, &plen);
    for (int i = 0; i < plen; ++i) {
        for (int j = i + 1; j < (plen-1); ++j) {
            if (primes[i] < 1000 || primes[j] < 1000 ||
                    primes[i] == 1487)
                continue;
            // primes[j] > primes[i]
            diff = primes[j] - primes[i];
            k = primes[j] + diff;
            if (k < 10000 && is_prime(primes, plen, k)) {
                i_s = split_number(primes[i], &i_l);
                j_s = split_number(primes[j], &j_l);
                if (are_perms49(i_s, j_s)) {
                    k_s = split_number(k, &k_l);
                    if (are_perms49(j_s, k_s)) {
                        printf("%d %d %d\n", primes[i], primes[j], k);
                        free(k_s);
                        free(j_s);
                        free(i_s);
                        return 0;
                    }
                    free(k_s);
                }
                free(j_s);
                free(i_s);
            }
        }
    }
    return 0;
}
Ejemplo n.º 3
0
 list_of_primes operator()(void) const
 {
   list_of_primes primes;
   primes.push_back(2);
   primes.push_back(3);
   return find_primes(primes, 5);
 }
Ejemplo n.º 4
0
 list_of_primes find_primes(const list_of_primes &primes, const number start) const
 {
   if (primes.size() < _max_number_of_primes)
   {
     if (is_prime(primes, start, 1, (number) (sqrt((double) start) + 0.5)))
     {
       list_of_primes new_list = primes;
       new_list.push_back(start);
       return find_primes(new_list, start+2);
     }
     else
       return find_primes(primes, start+2);
   }
   else
     return primes;
 }
Ejemplo n.º 5
0
int main()
{
    int n, m, i;
    find_primes(100);
    scanf("%d %d", &n, &m);
    for(i=0; prime[i]<=n; i++);
    printf("%s", prime[i]==m ? "YES" : "NO");
    return 0;    
}
Ejemplo n.º 6
0
Archivo: 3.c Proyecto: nuestand/Euler
int main(){
  find_primes(600851475143);
  return 0;
}
Ejemplo n.º 7
0
int
main (int argc, char *argv[])
{
  char *progname = argv[0];
  mpz_t fr, to;
  mpz_t fr2, to2;
  unsigned long sieve_lim;
  unsigned long est_n_primes;
  unsigned char *s;
  mpz_t tmp;
  mpz_t siev_sqr_lim;

  while (argc != 1)
    {
      if (strcmp (argv[1], "-c") == 0)
	{
	  flag_count = 1;
	  argv++;
	  argc--;
	}
      else if (strcmp (argv[1], "-p") == 0)
	{
	  flag_print = 2;
	  argv++;
	  argc--;
	}
      else if (strcmp (argv[1], "-g") == 0)
	{
	  flag_maxgap = 1;
	  argv++;
	  argc--;
	}
      else
	break;
    }

  if (flag_count || flag_maxgap)
    flag_print--;		/* clear unless an explicit -p  */

  mpz_init (fr);
  mpz_init (to);
  mpz_init (fr2);
  mpz_init (to2);

  if (argc == 3)
    {
      mpz_set_str (fr, argv[1], 0);
      if (argv[2][0] == '+')
	{
	  mpz_set_str (to, argv[2] + 1, 0);
	  mpz_add (to, to, fr);
	}
      else
	mpz_set_str (to, argv[2], 0);
    }
  else if (argc == 2)
    {
      mpz_set_ui (fr, 0);
      mpz_set_str (to, argv[1], 0);
    }
  else
    {
      fprintf (stderr, "usage: %s [-c] [-p] [-g] [from [+]]to\n", progname);
      exit (1);
    }

  mpz_set (fr2, fr);
  if (mpz_cmp_ui (fr2, 3) < 0)
    {
      mpz_set_ui (fr2, 2);
      report (fr2);
      mpz_set_ui (fr2, 3);
    }
  mpz_setbit (fr2, 0);				/* make odd */
  mpz_sub_ui (to2, to, 1);
  mpz_setbit (to2, 0);				/* make odd */

  mpz_init (tmp);
  mpz_init (siev_sqr_lim);

  mpz_sqrt (tmp, to2);
#define SIEVE_LIMIT 10000000
  if (mpz_cmp_ui (tmp, SIEVE_LIMIT) < 0)
    {
      sieve_lim = mpz_get_ui (tmp);
    }
  else
    {
      sieve_lim = SIEVE_LIMIT;
      mpz_sub (tmp, to2, fr2);
      if (mpz_cmp_ui (tmp, sieve_lim) < 0)
	sieve_lim = mpz_get_ui (tmp);	/* limit sieving for small ranges */
    }
  mpz_set_ui (siev_sqr_lim, sieve_lim + 1);
  mpz_mul_ui (siev_sqr_lim, siev_sqr_lim, sieve_lim + 1);

  est_n_primes = (size_t) (sieve_lim / log((double) sieve_lim) * 1.13) + 10;
  primes = malloc (est_n_primes * sizeof primes[0]);
  make_primelist (sieve_lim);
  assert (est_n_primes >= n_primes);

#if DEBUG
  printf ("sieve_lim = %lu\n", sieve_lim);
  printf ("n_primes = %lu (3..%u)\n",
	  n_primes, primes[n_primes - 1].prime);
#endif

#define S (1 << 15)		/* FIXME: Figure out L1 cache size */
  s = malloc (S/2);
  while (mpz_cmp (fr2, to2) <= 0)
    {
      unsigned long rsize;
      rsize = S;
      mpz_add_ui (tmp, fr2, rsize);
      if (mpz_cmp (tmp, to2) > 0)
	{
	  mpz_sub (tmp, to2, fr2);
	  rsize = mpz_get_ui (tmp) + 2;
	}
#if DEBUG
      printf ("Sieving region ["); mpz_out_str (stdout, 10, fr2);
      printf (","); mpz_add_ui (tmp, fr2, rsize - 2);
      mpz_out_str (stdout, 10, tmp); printf ("]\n");
#endif
      sieve_region (s, fr2, rsize);
      find_primes (s, fr2, rsize / 2, siev_sqr_lim);

      mpz_add_ui (fr2, fr2, S);
    }
  free (s);

  if (flag_count)
    printf ("Pi(interval) = %lu\n", total_primes);

  if (flag_maxgap)
    printf ("max gap: %lu\n", maxgap);

  return 0;
}
Ejemplo n.º 8
0
/*
 * Run multi-process prime finder program.
 */
int main(int argc, char **argv) {

    /* check if enough command line args have been supplied and exit on error if not */
    if (argc <= 1) {
    	printf("usage: ./primes <increasing positive integers>\n");
    	exit(-1);
    }

    int num_args = argc - 1;        /* number of arguements provided (number of numbers provided to check) */
    int nums[num_args];             /* array to hold the integer command line args */
    int fds[num_args][2];           /* file descriptor arrays for pipe */
    int read_fds[num_args];         /* file descriptors to read from */
    int max_fd;                     /* max fd to select */
    int bottom;                     /* bottom of range */
    int top;                        /* top of range */
    int child_count;                /* number of children for parent to keep track of */
    int read_buff;                  /* int buffer for reading primes from children */
    int i, n;
    pid_t pid;                      /* pid of the parent/child processes */
    pid_t pid_array[num_args];      /* array of child pids */
    char fifo_name[20];             /* buffer for creating the names for the FIFO's */

    /* get the integers that the user entered */
    for (i = 1 ; i < argc ; i++) {
        nums[i - 1] = atoi(argv[i]);
    }

    /* create pipes and FIFO's */
    for (i = 0 ; i < num_args ; i++) {
        if ((i + 1) % 2 != 0) {
            /* create pipes for odd children */

            if (pipe(fds[i]) < 0) {
                /* pipe error */
                perror("pipe");
                exit(-1);
            }

            /* load file descriptor to read from into read_fds */
            read_fds[i] = fds[i][0];

        } else {
            /* create FIFO's for even children */

            memset(fifo_name, '\0', 20 * sizeof(char));
            sprintf(fifo_name, "fifo%d", i);

            if (mkfifo(fifo_name, 0666) < 0) {
                /* fifo error */
                perror("mkfifo");
                exit(-1);
            }

            /* open fifo on parent end and load file descriptor to read from into read_fds */
            if ((read_fds[i] = open(fifo_name, O_RDONLY | O_NONBLOCK)) < 0) {
                perror("open");
                exit(-1);
            }
        }
    }

    /* create child processes and setup pipes/FIFO */
    for (i = 0, child_count = 0 ; i < num_args ; i++) {

    	/* odd child case - use pipe */
    	if ((i + 1) % 2 != 0) {

    	    if ((pid = fork()) < 0) {
        		/* fork error */
        		printf("\nERROR: Unable to fork.\n");
        		exit(-1);
    	    } else if (pid == 0) {
        		/* child */

                /* close read side of pipe for child */
                if (close(fds[i][0]) < 0) {
                    printf("\nERROR: child unable to close fd %d\n", fds[i][0]);
                    exit(-1);
                }

        		if (i == 0) {
        		    bottom = 2;
        		} else {
        		    bottom = nums[i - 1] + 1;
        		}
        		top = nums[i];

        		int num_primes = find_primes(bottom, top, fds[i][1]);

                /* printf("child %d: exiting with %d\n", (int)getpid(), num_primes); */

                /* exit with number of primes found */
        		exit(num_primes);

    	    } else {
                /* parent */
                pid_array[i] = pid;
                child_count++;

                /* close write side of pipe for parent */
                if (close(fds[i][1]) < 0) {
                    printf("\nERROR: parent unable to close fd %d\n", fds[i][1]);
                    exit(-1);
                }
    	    }

        /* even child case - use FIFO */
    	} else {

    	    if ((pid = fork()) < 0) {
        		/* error */
        		printf("\nERROR: Unable to fork.\n");
        		exit(-1);
    	    } else if (pid == 0) {
        		/* child */

        		if (i == 0) {
        		    bottom = 2;
        		} else {
        		    bottom = nums[i - 1] + 1;
        		}
        		top = nums[i];

                memset(fifo_name, '\0', 20 * sizeof(char));
                sprintf(fifo_name, "fifo%d", i);

                /* open fifo on child end and get the fd to write to */
                int write_fd;
                if ((write_fd = open(fifo_name, O_APPEND | O_WRONLY | O_NONBLOCK)) < 0) {
                    perror("open");
                    exit(-1);
                }

        		int num_primes = find_primes(bottom, top, write_fd);

                /* printf("child %d: exiting with %d\n", (int)getpid(), num_primes); */

                /* exit with number of primes found */
        		exit(num_primes);

    	    } else {
        		/* parent */
                pid_array[i] = pid;
                child_count++;

    	    }

    	}

    }

    /* set max fd to read from */
    for (i = 0, max_fd = 0 ; i < num_args ; i++) {
        if (max_fd < read_fds[i])
            max_fd = read_fds[i];
    }

    /* setup select */
    fd_set read_set;
    struct timeval timeout;

    timeout.tv_sec = 5;
    timeout.tv_usec = 0;

    int ret;
    pid_t exited_child;
    int exit_status;

    while (child_count) {

        FD_ZERO(&read_set);
        for (i = 0 ; i < num_args ; i++) {
            if (read_fds[i] != -1)
                FD_SET(read_fds[i], &read_set);
        }

        ret = select(max_fd + 1, &read_set, NULL, NULL, &timeout);

        /* no activity */
        if (ret == 0) {
            continue;
        }
        /* error */
        else if (ret < 0) {
            perror("select");
            exit(-1);
        }
        /* activity on some fds */
        else {
            /* check which fds were written to */
            for (i = 0 ; i < num_args; i++) {

                /* if the file descriptor associated with the child is set, read from it */
                if (FD_ISSET(read_fds[i], &read_set)) {

                    /* read all primes from child and print to screen if there are any */
                    while (1) {
                        if ((n = read(read_fds[i], &read_buff, sizeof(int))) < 0) {
                            /* if failed read is just because the file is being used by a child, ignore it */
                            if (errno != 11) {
                                perror("read");
                                exit(-1);
                            }
                        } else if (n > 0) {
                            printf("%d is prime from %d\n", read_buff, pid_array[i]);
                        } else {
                            break;
                        }
                    }

                    /* check if child has exited if it hasn't already been confirmed exited */
                    if ((exited_child = waitpid(pid_array[i], &exit_status, WNOHANG)) != 0) {
                        if (exited_child < 0) {
                            perror("waitpid");
                            exit(-1);
                        } else {
                            /* remove fifo file if even child */
                            if ((i + 1) % 2 == 0) {
                                memset(fifo_name, '\0', 20 * sizeof(char));
                                sprintf(fifo_name, "fifo%d", i);

                                if (remove((const char*)fifo_name) < 0) {
                                    perror("remove");
                                    exit(-1);
                                }
                            }

                            /* decrement child count, "remove" child's pid from pid_array, and print exit status */
                            child_count--;
                            close(read_fds[i]);
                            read_fds[i] = -1;
                            printf("child %d exited correctly and found %d primes\n", (int)exited_child, WEXITSTATUS(exit_status));
                        }
                    }
                }

            }
        }
    }

    return 0;
}