In: Computer Science
I need this code translated to C code. Thank you.
public class FourColorTheorem {
public static boolean isPrime(int num) {
// Corner case
if (num <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < num; i++)
if (num % i == 0)
return false;
return true;
}
public static void main(String[] args) {
int squares[] = new int[100];
for (int i = 1; i < squares.length; i++) squares[i-1] = i *
i;
int n = 5;
while (true) {
boolean flag = false;
for (int i = 0; i < squares.length; i++) {
int difference = n - 2 * squares[i];
if (isPrime(difference)) {
flag = true;
// you can comment the below line
System.out.println(n + " = " + difference + " + 2*" +
Math.sqrt(squares[i]) + "*" + Math.sqrt(squares[i]));
break;
}
}
if (!flag) {
break;
}
n += 2;
}
System.out.println("Smallest counter example = " + n);
}
}
#include <stdio.h>
int isPrime(int num) {
// Corner case
int i;
if (num <= 1)
return 0;
// Check from 2 to n-1
for (i = 2; i < num; i++)
if (num % i == 0)
return 0;
return 1;
}
int main() {
int squares[100],i,n,flag,difference;
for (i = 1; i < 100; i++)
squares[i-1] = i * i;
n = 5;
while (1) {
flag = 0;
for (i = 0; i < 100; i++) {
difference = n - 2 * squares[i];
if (isPrime(difference)) {
flag = 1;
break;
}
}
if (!flag) {
break;
}
n += 2;
}
printf("Smallest counter example = %d" , n);
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASE_700/final) : clang++-7 -pthread -o main main.cpp ../main Smallest counter example = 57770