Task: Long primes


C++: #include <iomanip>
#include <iostream>
#include <list>
using namespace std;
  
void sieve(int limit, list<int> &primes)
{
  bool *c = new bool[limit + 1];
  for (int i = 0; i <= limit; i++)
    c[i] = false;
  
  int p = 3, n = 0;
  int p2 = p * p;
  while (p2 <= limit)
  {
    for (int i = p2; i <= limit; i += 2 * p)
      c[i] = true;
    do
      p += 2;
    while (c[p]);
    p2 = p * p;
  }
  for (int i = 3; i <= limit; i += 2) 
    if (!c[i]) 
      primes.push_back(i);
  delete [] c;
}
 

int findPeriod(int n) 
{
  int r = 1, rr, period = 0;
  for (int i = 1; i <= n + 1; ++i)
    r = (10 * r) % n;
  rr = r;
  do
  {
    r = (10 * r) % n;
    period++;
  }
  while (r != rr);
  return period;
}
 
int main()
{
  int count = 0, index = 0;
  int numbers[] = {500, 1000, 2000, 4000, 8000, 16000, 32000, 64000};
  list<int> primes;
  list<int> longPrimes;
  int numberCount = sizeof(numbers) / sizeof(int);
  int *totals = new int[numberCount];
  cout << "Please wait." << endl << endl;
  sieve(64000, primes);  
  for (list<int>::iterator iterPrime = primes.begin();  
    iterPrime != primes.end(); 
    iterPrime++)
  {  
    if (findPeriod(*iterPrime) == *iterPrime - 1)
      longPrimes.push_back(*iterPrime);
  }    
  
  for (list<int>::iterator iterLongPrime = longPrimes.begin();
    iterLongPrime != longPrimes.end(); 
    iterLongPrime++)
  {
    if (*iterLongPrime > numbers[index])
      totals[index++] = count;
        ++count;
  }
  totals[numberCount - 1] = count;
  cout << "The long primes up to " << totals[0] << " are:" << endl;
  cout << "[";
  int i = 0;
  for (list<int>::iterator iterLongPrime = longPrimes.begin(); 
    iterLongPrime != longPrimes.end() && i < totals[0]; 
    iterLongPrime++, i++)
  {  
    cout << *iterLongPrime << " ";
  }  
  cout << "\b]" << endl;
  cout << endl << "The number of long primes up to:" << endl;
  for (int i = 0; i < 8; ++i)
    cout << "  " << setw(5) << numbers[i] << " is " << totals[i] << endl;
  delete [] totals;
}



Go: package main
 
import "fmt"
 
func sieve(limit int) []int {
    var primes []int
    c := make([]bool, limit + 1) 
    
    p := 3
    p2 := p * p
    for p2 <= limit {
        for i := p2; i <= limit; i += 2 * p {
            c[i] = true
        }
        for ok := true; ok; ok = c[p] {
            p += 2
        }
        p2 = p * p
    }
    for i := 3; i <= limit; i += 2 {
        if !c[i] {
            primes = append(primes, i)
        }
    }
    return primes
}
 

func findPeriod(n int) int {
    r := 1
    for i := 1; i <= n + 1; i++ {
        r = (10 * r) % n
    }
    rr := r
    period := 0
    for ok := true; ok; ok = r != rr {
        r = (10 * r) % n
        period++
    }
    return period
}
 
func main() {
    primes := sieve(64000)
    var longPrimes []int
    for _, prime := range primes {
        if findPeriod(prime) == prime - 1 {
            longPrimes = append(longPrimes, prime)
        }
    }
    numbers := []int{500, 1000, 2000, 4000, 8000, 16000, 32000, 64000}
    index := 0
    count := 0
    totals := make([]int, len(numbers))
    for _, longPrime := range longPrimes {
        if longPrime > numbers[index] {
            totals[index] = count
            index++
        }
        count++
    }
    totals[len(numbers)-1] = count
    fmt.Println("The long primes up to", numbers[0], "are: ")
    fmt.Println(longPrimes[:totals[0]])
 
    fmt.Println("\nThe number of long primes up to: ")
    for i, total := range totals {
        fmt.Printf("  %5d is %d\n", numbers[i], total)
    }
}