Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Goldbach Conjecture
#include <stdio.h> #include <stdlib.h> int IsPrime(int number) { // Check if a number is prime int i = 0; for (i = 2; i < number; i++) { if (number % i == 0 && i != number) { return 0; } } return 1; } int IsEven(int number) { // Check if a number is even if (number % 2) { // Number is odd return 0; } else { // Number is even return 1; } } int IsRange(int number) { if (number >= 2) { return 1; } else { return 0; } } int ArrayLength(int array[]) { int size = (sizeof array) / (sizeof array[0]); return size; } int HowManyPrimes (int number) { int numberOfValues = 0, i = 0; for (i = 2; i < number; i++) { if (IsPrime(i) == 1) { numberOfValues++; } } return numberOfValues; } int main(void) { // 1. Input Number int number = 0; printf("Please enter a number from 2 to plus infinite that is even:\n"); //scanf("%d", &number); // 2. Check if number meets criteria if (number == 2) { printf("The answer is:\n\n"); printf(" --> (1,1) "); printf("Although 1 is not a prime number, it was considered as such when Goldbach made his conjecture!\n"); printf("\nThere was a total of 1 unique answer!\n"); return 0; } if (IsEven(number) == 0 || IsRange(number) == 0) { printf("The number is wether not within range, or not even. Please, enter a number between 2 and +infinite that is even!\n"); return 0; } // 3. Generating an array of prime number at max as big as the number int nbPrimes = HowManyPrimes(number), primes[nbPrimes], i = 0, j = 0, numberOfPair = 0; printf("The function is calculating! This may take a while...\n"); for (i = 2; i < number; i++) { if (IsPrime(i) == 1) { primes[j] = i; j++; } } // 4. Proceed numbers in array to check the possibilities and printing results printf("The computation is complete, here are the answer(s):\n\n"); for (i = 0; i < nbPrimes; i++) { int reminder = abs(number - primes[i]); if (IsPrime(reminder) == 1 && reminder != 1 && reminder >= primes[i]) { printf(" --> (%d,%d)\n", primes[i], reminder); numberOfPair++; } } printf("\nThere was a total of %d unique answer(s)!\n", numberOfPair); return 0; }
run
|
edit
|
history
|
help
0
linear hybrid cellular automaton reversible random bit generator
array
if con boleanos 01
string divison
03 Muestra enteros y flotante
objective c find factorial
find maximum in objective c
Inheritance Example
Convert String to NSData Json Object
Objective C NSSet Declaration