Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CollatzConjecture #444

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions cantSolve/collatzConjecture.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdbool.h>

// Function to check if a number is prime
bool isPrime(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}

// Function to find two prime numbers that sum up to n (Goldbach Conjecture)
void goldbachConjecture(int n) {
if (n <= 2 || n % 2 != 0) {
printf("Goldbach Conjecture is only applicable to even numbers greater than 2.\n");
return;
}
for (int i = 2; i <= n / 2; i++) {
if (isPrime(i) && isPrime(n - i)) {
printf("%d = %d + %d\n", n, i, n - i);
return;
}
}
printf("Goldbach Conjecture is false for %d!\n", n);
}

int main() {
int limit;

printf("Enter the limit for even numbers (greater than 2): ");
scanf("%d", &limit);

if (limit <= 2 || limit % 2 != 0) {
printf("Invalid input. Please enter an even number greater than 2.\n");
return 1;
}

for (int i = 4; i <= limit; i += 2) {
goldbachConjecture(i);
}

return 0;
}
1 change: 1 addition & 0 deletions v_proglang/geo
Submodule geo added at 7bf5bd