Code in English — Part 2

Panna Lal Patodia
9 min readSep 11, 2022

--

Generate Code from Plain English in Multiple Languages

“Code in English” is the new paradigm of software development

https://www.fastdevai.com

AI Code Generator from English Description

Introduction:

In Part 1, we discussed how we can create programs in different languages from English descriptions. We have introduced a revolutionary product “Code in English” that can generate code from plain English in multiple languages. Currently, we support Python, JavaScript, Java, C, C++, C#, PHP, Go, Perl, Ruby, Typescript, Haskell, Kotlin, Scala, Swift and Dart languages. This improves the productivity of the software developers by 2 to 3 times. It can help immensely by reducing the cost, improving profitability, and delivering the product much faster. It will also help software developers of your company to learn new programming languages in a very innovative way. We have displayed programs in Python, PHP, C++, Java, and JavaScript created from the same English description.

In this part, we will not concentrate on multiple languages. Instead, we will take a single problem and see how we can optimize the program just by changing the description in plain English. This will serve two purposes:

(1) It is easy to optimize the programs just by making changes in the English description.

(2) It is easier to maintain and change the program when we keep the program in English rather than maintaining and changing the code in programming languages.

Let us describe the problem in plain English:

Accept n as first argument of command line, convert it to long long, and then compute its prime factors. If user has not entered the first argument, provide the usage message and exit.

Let us generate program in C++ from the above description and save it as factor1.cpp:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;int main(int argc, char *argv[])
{
if (argc != 2)
{
cout << "Usage: " << argv[0] << " number" << endl;
return 1;
}
long long n = atoll(argv[1]); for (long long i = 2; i <= sqrt(n); i++)
{
while (n % i == 0)
{
cout << i << " ";
n /= i;
}
}
if (n > 1)
{
cout << n << endl;
}
return 0;
}

We have compiled the above program and it works perfectly except that it cannot factorize negative numbers. Further, it keeps checking even numbers. As 2 is the only even prime, we can make it much faster by dividing n only by odd numbers except 2. So, we change the description as follows:

Create a function factor(long long n) based on the following:
(1) If n is negative, display -1 and take absolute value of n.
(2) If n is less than 4, display n and return.
(3) while n is divisible by 2, display 2 and divide n by 2.
(4) assign long j = sqrt(n+1).
(5) loop value of i from 3 to j incrementing i by 2 at each iteration. While n is divisible by i, display i, divided n by i and assign j = sqrt(n+1).
(6) if n is greater than 2, display n and return from the function.

Accept n as first argument of command line, convert it to long long and then call factor(n). If user has not entered the first argument, provide the usage message and exit.

Based on the above we have generated C++ program and saved it as factor2.cpp:

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;void factor(long long n)
{
if (n < 0)
{
cout << -1 << " ";
n = abs(n);
}
if (n < 4)
{
cout << n << endl;
return;
}
while (n % 2 == 0)
{
cout << 2 << " ";
n /= 2;
}
long j = sqrt(n + 1);
for (long i = 3; i <= j; i += 2)
{
while (n % i == 0)
{
cout << i << " ";
n /= i;
j = sqrt(n + 1);
}
}
if (n > 2)
cout << n << endl;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <number>" << endl;
return 1;
}
long long n = atoll(argv[1]);
factor(n);
return 0;
}

Now, we can factor negative numbers. Further its speed has doubled. For example, if we factor -12, we get prime factors: -1 2 2 3.

In the above program, we are avoiding checking division with 2. Let us also avoid checking division by all numbers which are divisible by 3. So, we change the description accordingly. The new description is given below:

Create a function factor(long long n) based on the following:
(1) If n is negative, display -1 and take absolute value of n.
(2) If n is less than 4, display n and return.
(3) while n is divisible by 2, display 2 and divide n by 2.
(4) while n is divisible by 3, display 3 and divide n by 3.
(5) assign long j = sqrt(n+1).
(6) loop value of i from 5 to j incrementing i by 6 at each iteration. While n is divisible by i, display i, divided n by i and assign j = sqrt(n+1). While n is divisible by i+2, display i+2, divide by i+2 and assign j = sqrt(n+1).
(7) if n is greater than 2, display n and return from the function.

Accept n as first argument of command line, convert it to long long and then call factor(n). If user has not entered the first argument, provide the usage message and exit.

Based on the above we have generated C++ program and saved it as factor3.cpp:

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;void factor(long long n)
{
if (n < 0)
{
cout << -1 << " ";
n = abs(n);
}
if (n < 4)
{
cout << n << endl;
return;
}
while (n % 2 == 0)
{
cout << 2 << " ";
n /= 2;
}
while (n % 3 == 0)
{
cout << 3 << " ";
n /= 3;
}
long long j = sqrt(n + 1);
for (long long i = 5; i <= j; i += 6)
{
while (n % i == 0)
{
cout << i << " ";
n /= i;
j = sqrt(n + 1);
}
while (n % (i + 2) == 0)
{
cout << i + 2 << " ";
n /= i + 2;
j = sqrt(n + 1);
}
}
if (n > 2)
{
cout << n << endl;
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <number>" << endl;
return 1;
}
long long n = atoll(argv[1]);
factor(n);
return 0;
}

The above program is further optimized. It should be noted that we have not changed a single character of code, just slight change in the English description and the optimized code is generated. This shows the power of the tool.

Finally, we want to display the time taken in factorization of a number. So, we added the following line:

Compute the time taken by factor(long long n) in milliseconds.

At the cost of repetition, let us give the complete English description for the final program:

Create a function factor(long long n) based on the following:
(1) If n is negative, display -1 and take absolute value of n.
(2) If n is less than 4, display n and return.
(3) while n is divisible by 2, display 2 and divide n by 2.
(4) while n is divisible by 3, display 3 and divide n by 3.
(5) assign long j = sqrt(n+1).
(6) loop value of i from 5 to j incrementing i by 6 at each iteration. While n is divisible by i, display i, divided n by i and assign j = sqrt(n+1). While n is divisible by i+2, display i+2, divide by i+2 and assign j = sqrt(n+1).
(7) if n is greater than 2, display n and return from the function.

Accept n as first argument of command line, convert it to long long and then call factor(n). If user has not entered the first argument, provide the usage message and exit. Compute the time taken by factor(long long n) in milliseconds.

We have generated the final C++ program from the above description and saved it as factor4.cpp:

#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;void factor(long long n)
{
if (n < 0)
{
cout << -1 << " ";
n = abs(n);
}
if (n < 4)
{
cout << n << endl;
return;
}
while (n % 2 == 0)
{
cout << 2 << " ";
n /= 2;
}
while (n % 3 == 0)
{
cout << 3 << " ";
n /= 3;
}
long long j = sqrt(n + 1);
for (long long i = 5; i <= j; i += 6)
{
while (n % i == 0)
{
cout << i << " ";
n /= i;
j = sqrt(n + 1);
}
while (n % (i + 2) == 0)
{
cout << i + 2 << " ";
n /= i + 2;
j = sqrt(n + 1);
}
}
if (n > 2)
cout << n << endl;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <number>" << endl;
return 1;
}
long long n = stoll(argv[1]);
clock_t start = clock();
factor(n);
clock_t end = clock();
cout << "Time taken: " << (end - start) / (double)CLOCKS_PER_SEC * 1000 << " ms" << endl;
return 0;
}

Below, we give some examples:

C:\Programming_Solutions>factor4.exe 123456789012359817
3 3 7 2944099 665613341
Time taken: 10 ms

C:\Programming_Solutions>factor4.exe 123456789012359819
23 71 11677 6474369959
Time taken: 0 ms

C:\Programming_Solutions>factor4.exe 123456789012359829
3 13 59 101 531223140229
Time taken: 10 ms

The program is so efficient that we can factor 18-digit numbers in 10 milliseconds (1/100 second).

It should be noted that we are using the word “long long” specifically used in C++. Can we generate Java program from the English description given above. Let us try and see the result:

import java.util.Scanner;
import java.lang.Math;
import java.lang.System;
public class Factor {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java Factor <number>");
System.exit(0);
}
long n = Long.parseLong(args[0]);
long startTime = System.currentTimeMillis();
factor(n);
long endTime = System.currentTimeMillis();
System.out.println("Time taken: " + (endTime - startTime) + " ms");
}
public static void factor(long n) {
if (n < 0) {
System.out.println(-1);
n = Math.abs(n);
}
if (n < 4) {
System.out.println(n);
return;
}
while (n % 2 == 0) {
System.out.println(2);
n /= 2;
}
while (n % 3 == 0) {
System.out.println(3);
n /= 3;
}
long j = (long) Math.sqrt(n + 1);
for (long i = 5; i <= j; i += 6) {
while (n % i == 0) {
System.out.println(i);
n /= i;
j = (long) Math.sqrt(n + 1);
}
while (n % (i + 2) == 0) {
System.out.println(i + 2);
n /= (i + 2);
j = (long) Math.sqrt(n + 1);
}
}
if (n > 2) {
System.out.println(n);
}
}
}

We could generate java program. The class name is “Factor”, so we saved it as Factor.java. We have compiled and run the program with the same input. Below, we display the result:

C:\Programming_Solutions>java Factor 123456789012359817
3
3
7
2944099
665613341
Time taken: 30 ms

C:\Programming_Solutions>java Factor 123456789012359819
23
71
11677
6474369959
Time taken: 10 ms

C:\Programming_Solutions>java Factor 123456789012359829
3
13
59
101
531223140229
Time taken: 20 ms

As Java program is comparatively slower, it takes between 10 milliseconds and 30 milliseconds. It is still fast enough to factorize 18-digit numbers in .03 second. There is no difference in the algorithm as Java program is generated from the same English description, it is just slower in comparison of C++.

Most Important Note:

All the above programs are generated from plain English description, and we have not changed a single character in any of the above program. This shows the power of “Code in English” and ease of maintenance. Whenever changes are required in the program, take the English description (this we keep as comment on top of each program), change it and generate new code.

Thanks so much for reading my article!!! I request readers to put forth their valuable comments. This gives me encouragement. I am also thankful to all the readers for their valuable comments.

For further details, please visit the website https://www.fastdevai.com

--

--

Panna Lal Patodia
Panna Lal Patodia

Written by Panna Lal Patodia

CEO of Patodia Infotech Private Limited.

No responses yet