Prime Generator in C
Off topic → Programming → Prime Generator in C
Lately, I’ve been doing mathematical experiments in the field of prime numbers. So, I’ve developed a program that will generate primes and write the primes to a TXT file.
#include <stdio.h>
void main() {
int count1 = 2;
int count2 = 2;
int primes[100000];
int is_prime = 1;
int prime_count = 1;
int count = 1;
printf("Press ENTER to start calculation of primes (CPU intensive and will take at most about 30 sec.).");
getchar();
printf("\n");
while(count1 <= 100000) {
while(count2 < count1) {
if(count1 % count2 == 0) {
is_prime = 0;
}
count2 += 1;
}
count2 = 2;
if(is_prime == 1) {
primes[prime_count] = count1;
prime_count += 1;
}
is_prime = 1;
count1 += 1;
}
count1 = 1;
count2 = 1;
is_prime = 0;
printf("Calculation finished. Press ENTER to write primes to TXT.");
getchar();
printf("\n");
FILE *file = fopen("c:\\users\\[SOME USER]\\desktop\\c\\primes.txt", "w+");
while(count <= 1000) {
fprintf(file, "%i\n", primes[count]);
count += 1;
}
count = 1;
printf("Writing to TXT finished; end of program. Press ENTER to exit.");
getchar();
fclose(file);
}
Feel completely free to use or modify this code. This isn’t LinnyCopyrighted.
Here is my Prime generating code, for Python, just because I don’t know C…:
Input=100000
primes=[]
fo = open("output.txt", "w+")
print("Press ENTER to start calculation of primes (CPU intensive and will take at most about TODO: Write an approximation sec.).");
input()
for x in range(2,Input+1):
IsPrime=True
for y in primes:
if(x%y==0):
IsPrime = False
if IsPrime:
primes.append(x)
fo.write(str(x)+"\n")
fo.close()
lol… I’ve worked in Python before, and it’s fine, but working in low-level programming languages can be easier for me, because I usually understand things closer to machine code.
fml typing this in a few meters away from the fucking sicking dog diahorrea shit stench of sea cucumbers that my parents are drying out (it’s winter, so no sun to dry it, so it’s indoors)
fml