Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Goldbach Conjecture
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#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; }
gcc
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.12 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 4 Mb, absolute service time: 0.26 sec
edit mode
|
history
|
discussion