Project Euler #58: Spiral primes

Sort by

recency

|

32 Discussions

|

  • + 0 comments

    Python

    import random
    
    def isPrimeMillerRabin(n, k=3):
        if n <= 1:
            return False
        if n == 2 or n == 3:
            return True
        if n % 2 == 0:
            return False
    
        r, d = 0, n - 1
        while d % 2 == 0:
            r += 1
            d //= 2
    
        # Perform Miller-Rabin test k times
        for _ in range(k):
            a = random.randint(2, n - 2)
            x = pow(a, d, n)
    
            if x == 1 or x == n - 1:
                continue
    
            for _ in range(r - 1):
                x = pow(x, 2, n)
                if x == n - 1:
                    break
            else:
                return False  
    
        return True  # Likely to be prime
    
    
    N = float(input()) / 100
    count = 0
    i = 3  
    spiral_size = 1
    
    while True:
        for j in range(4):
            num = i**2 - (i - 1) * j
            if isPrimeMillerRabin(num):
                count += 1
                
        spiral_size += 4
        proportion = count / spiral_size
    
        if proportion < N:
            break
    
        i += 2  
    
    print(i)
    
  • + 0 comments

    I can't believe I spent so long time debugging until I realize I seived 1 as prime : (

  • + 0 comments

    JAva code

    7- 8 cash are not pass

     import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
              Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            scanner.close();
    
            int primesCount = 0;
            int i = 3;
            int lastNumber = 1;
    
            while (true) {
                for (int j = 0; j < 4; j++) {
                    lastNumber += i - 1;
                    if (j != 3 && isPrime(lastNumber)) {
                        primesCount++;
                    }
                }
    
                double primesPercentage = (double) primesCount / (1 + 2 * (i - 1)) * 100;
                if (primesPercentage < n) {
                    System.out.println(i);
                    break;
                }
                i += 2;
            }
        }
    
        public static boolean isPrime(int n) {
            if (n <= 1) {
                return false;
            }
            if (n <= 3) {
                return true;
            }
            if (n % 2 == 0 || n % 3 == 0) {
                return false;
            }
    
            int d = n - 1;
            int s = 0;
            while (d % 2 == 0) {
                s++;
                d /= 2;
            }
    
            Random random = new Random();
            for (int i = 0; i < 4; i++) {
                int a = random.nextInt(n - 2) + 2;
                int x = modPow(a, d, n);
                if (x != 1 && x != n - 1) {
                    boolean active = true;
                    for (int r = 0; r < s; r++) {
                        x = modPow(x, 2, n);
                        if (x == n - 1) {
                            active = false;
                            break;
                        }
                    }
                    if (active) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        public static int modPow(int base, int exponent, int mod) {
            int result = 1;
            base %= mod;
            while (exponent > 0) {
                if (exponent % 2 == 1) {
                    result = (int) ((long) result * base % mod);
                }
                base = (int) ((long) base * base % mod);
                exponent /= 2;
            }
            return result;
        }
    }
    
  • + 0 comments

    Can someone check/confirm the additional values for N < 8 that I found with my program (I use deterministic Miller-Rabin with the Sinclair base 2, 325, 9375, 28178, 450775, 9780504, 1795265022, see https://miller-rabin.appspot.com) ?

    for N = 7: 1'213'001 (takes around 1.8 seconds) for N = 6: 10'273'345 (takes around 17 seconds) for N = 5: 204'066'345 (already takes 6 minutes to run)

  • + 0 comments

    1)Miller Rabin is god
    2) Rest can be done using generalising the corner terms in spiral and using a while loop for increasing side by 2 till the req result in achieved.
    3) I also searched for any hints in ulam spiral, for any pattern, but couldn't find any.


    import random
    
    def isprime(n):
        '''Miller rabbin'''
        if n in [2,3,5,7]:
            return True
        
        d=n-1
        s=0
        while not d%2:
            s+=1
            d=d//2
        for i in range(4):
            a=random.randint(2,n-1)
            x=pow(a,d,n)
            if x not in [1,n-1]:
                active=True
                for r in range(s):
                    if pow(x,2**r,n) == n-1:
                        active=False
                        break
                if active:
                    return False
        return True
    
            
                
    n=int(input())
    primes_count=0
    i=3
    last_no=1
    
    while True:
        for j in range(4):
            last_no+=i-1
            if j!=3 and isprime(last_no):
                primes_count+=1
        
        primes_percentage=primes_count/(1+2*(i-1))*100
        if primes_percentage<n:
            print(i)
            break
        i+=2