Question

In: Computer Science

I need this code translated to C code. Thank you. public class FourColorTheorem { public static...

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);
}
}

Solutions

Expert Solution

#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


Related Solutions

I need this Java code translated into C Code. Thank you. //Logical is the main public...
I need this Java code translated into C Code. Thank you. //Logical is the main public class public class Logical { public static void main(String args[]) { char [][] table= {{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}}; // table contains total combinations of p,q,& r int totalTrue, totalFalse, proposition;    //proposition 1: proposition=1; start(proposition); totalTrue=0; totalFalse=0; for(int i=0;i<8;i++) {    { char o= conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0])); System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o); if(o=='T') totalTrue++; else totalFalse++;    } } finalOutput(totalTrue,totalFalse,proposition); System.out.println(" "); System.out.println(" ");    //proposition 2: proposition=2; start(proposition);...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
Java question: I need to fix a point class (code below) Thank you! /** * A...
Java question: I need to fix a point class (code below) Thank you! /** * A point, implemented as a location without a shape. */ public class Point extends Location { // TODO your job // HINT: use a circle with radius 0 as the shape! public Point(final int x, final int y) { super(-1, -1, null); assert x >= 0; assert y >= 0; } }
This is the code that needs to be completed... Thank you! public class MultiplicationTable { private...
This is the code that needs to be completed... Thank you! public class MultiplicationTable { private int[][] table; private int rows; private int cols;    /* Instantiate the two dimensional array named table. * Assign the numRows parameter to the rows field. * Assign the numCols parameter in the cols field. */ public MultiplicationTable(int numRows, int numCols) {    } /* Using nested for loops, fill the table array with * the values shown in the Multiplication Table document. */...
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will...
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will represent Fractions. Note: this is the first part of a multi-part assignment. For this week you should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. You must provide...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of...
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[]...
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[] args) {        // Define the input string. Note that I could have written it all on a        // single line, but I broke it up to match the question.        String input = "Book , Cost , Number\n"                    + "Hamlet , 24.95 , 10\n"                    + "King Lear , 18.42...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT