Railsbin – The vulnerable pastebin service!

User:

Title: Chatty

Content:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>

#define DEFAULT_LIMIT 10000
#define DEFAULT_NUM_THREADS 4

// Struct to hold thread arguments
typedef struct {
    bool *primes;
    int start;
    int end;
    int *counts;
    int step;
} ThreadArgs;

// Function to pre-compute primes using Sieve of Eratosthenes
void sieveOfEratosthenes(bool *primes, long long limit) {
    for (long long i = 0; i <= limit; i++) {
        primes[i] = true;
    }
    primes[0] = primes[1] = false;

    for (long long p = 2; p * p <= limit; p++) {
        if (primes[p]) {
            for (long long i = p * p; i <= limit; i += p) {
                primes[i] = false;
            }
        }
    }
}

// Function to count the number of combinations for a range of even numbers
void *countCombinationsInRange(void *arguments) {
    ThreadArgs *args = (ThreadArgs *)arguments;
    bool *primes = args->primes;
    int *counts = args->counts;

    int end = args->end;
    int step = args->step;

    for (int evenNum = args->start; evenNum <= end; evenNum += step) {
        int count = 0;
        int end2 = evenNum / 2;
        // Iterate through all possible numbers up to evenNum / 2
        for (int i = 2; i <= end2; i++) {
            // Check if both i and (evenNum - i) are prime
            if (primes[i] && primes[evenNum - i]) {
                count++;
            }
        }
        counts[(evenNum - 4) / 2] = count;
    }

    return (NULL);
}

int main(int argc, char *argv[]) {
    long long limit = DEFAULT_LIMIT;
    int num_threads = DEFAULT_NUM_THREADS;

    if (argc >= 2) {
        limit = atoll(argv[1]);
    }

    if (argc >= 3) {
        num_threads = atoi(argv[2]);
    }

    bool *primes = (bool *)malloc((limit + 1) * sizeof(bool));
    if (primes == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        return 1;
    }

    pthread_t *threads = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
    if (threads == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        free(primes);
        return 1;
    }

    int *counts = (int *)malloc(((limit - 2) / 2) * sizeof(int));
    if (counts == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        free(primes);
        free(threads);
        return 1;
    }

    sieveOfEratosthenes(primes, limit);

    ThreadArgs *threadArgs = (ThreadArgs *)malloc(num_threads * sizeof(ThreadArgs));
    if (threadArgs == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        free(primes);
        free(threads);
        free(counts);
        return 1;
    }

    for (int i = 0; i < num_threads; i++) {
        threadArgs[i].primes = primes;
        threadArgs[i].start = 4 + i * 2;
        threadArgs[i].end = limit;
        threadArgs[i].counts = counts;
        threadArgs[i].step = num_threads;
        pthread_create(&threads[i], NULL, countCombinationsInRange, (void *)&threadArgs[i]);
    }

    for (int i = 0; i < num_threads; i++) {
        pthread_join(threads[i], NULL);
    }

    // Print the counts in order
    for (int i = 0; i < ((limit - 2) / 2); i++) {
    	printf("%d", counts[i]);
//        printf("For even number %d, number of different combinations: %d\n", 4 + i * 2, counts[i]);
    }

    free(primes);
    free(threads);
    free(counts);
    free(threadArgs);

    return 0;
}

Edit | Back