Skip to main content
Logo image

PreTeXt Sample Book: Abstract Algebra (SAMPLE ONLY)

Section 3.3 Code Lens

View Source for section
<section xml:id="code-lens">
    <title>Code Lens</title>
    <p>
      CodeLens is an interactive tool for following program execution,
      much like a debugger,
      without the ability to influence flow control or variable values.
      For use without a server, traces must be computed beforehand.
      First, we have some trivial programs,
      to provide minimal testing.
    </p>
<listing xml:id="program-codelens-python">
<caption>A Python program, stepable with CodeLens</caption>
<program xml:id="python-hello-world-code-lens" interactive="codelens" language="python">
<code>
print('Hello, World!')
</code>
</program>
</listing>
<listing xml:id="program-codelens-c">
<caption>An C program, stepable with CodeLens</caption>
<program xml:id="c-hello-world-code-lens" interactive="codelens" language="c">
<code>
#include &lt;stdio.h&gt;
int main(void)
{
puts("Hello, World!");
}
</code>
</program>
</listing>
<listing xml:id="program-codelens-java">
<caption>A Java program, stepable with CodeLens</caption>
<program xml:id="java-hello-world-code-lens" interactive="codelens" language="java">
<code>
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
</code>
</program>
</listing>
    <p>
      Now some moderately more complicated programs to find teh prime numbers less than <m>20</m>.
      We do not vouch for the quality of these,
      or even their correctness!
    </p>
<listing xml:id="sieve-python">
<caption><url href="https://www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes" visual="www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes">Sieve of Eratosthenes</url>, Java</caption>
<program xml:id="sieve-codelens-python" interactive="codelens" language="python">
<code>
def SieveOfEratosthenes(n):
# array of type boolean with True values in it
prime = [True for i in range(n + 1)]
p = 2
while (p * p &lt;= n):
# If it remain unchanged it is prime
if (prime[p] == True):
# updating all the multiples
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
# Print
for p in range(n + 1):
if prime[p]:
print (p,end=" ")
# main
if __name__=='__main__':
n = 20
print ("The prime numbers smaller than or equal to", n,"is")
SieveOfEratosthenes(n)

</code>
</program>
</listing>
<listing xml:id="sieve-cpp">
<caption><url href="https://www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range" visual="www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range">Sieve of Eratosthenes</url>, C++</caption>
<program xml:id="sieve-codelens-cpp" interactive="codelens" language="cpp">
<code> 
#include &lt;stdio.h&gt;
const int len = 20;
int main() {
int arr[20] = {0};
for (int i = 2; i &lt; len; i++) {
for (int j = i * i; j &lt; len; j+=i) {
arr[j - 1] = 1;
}
}
for (int i = 1; i &lt; len; i++) {
if (arr[i - 1] == 0)
printf(" %d", i);
}
}

</code>
</program>
</listing>
<listing xml:id="sieve-java">
<caption><url href="https://www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java" visual="www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java">Sieve of Eratosthenes</url>, Java</caption>
<program xml:id="sieve-codelens-java" interactive="codelens" language="java">
<code>
public class SievePrimeFactors  {
public static void main(String args[]) {
int num = 20;
boolean[] bool = new boolean[num];
for (int i = 0; i&lt; bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i &lt; Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j &lt; num; j = j+i) {
bool[j] = false;
}
}
}
System.out.println("List of prime numbers: ");
for (int i = 2; i&lt; bool.length; i++) {
if(bool[i]==true) {
System.out.println(i);
}
}
}
}

</code>
</program>
</listing>
  </section>
CodeLens is an interactive tool for following program execution, much like a debugger, without the ability to influence flow control or variable values. For use without a server, traces must be computed beforehand. First, we have some trivial programs, to provide minimal testing.
View Source for listing
<listing xml:id="program-codelens-python">
<caption>A Python program, stepable with CodeLens</caption>
<program xml:id="python-hello-world-code-lens" interactive="codelens" language="python">
<code>
print('Hello, World!')
</code>
</program>
</listing>
View Source for program
<program xml:id="python-hello-world-code-lens" interactive="codelens" language="python">
<code>
print('Hello, World!')
</code>
</program>
Listing 3.3.1. A Python program, stepable with CodeLens
View Source for listing
<listing xml:id="program-codelens-c">
<caption>An C program, stepable with CodeLens</caption>
<program xml:id="c-hello-world-code-lens" interactive="codelens" language="c">
<code>
#include &lt;stdio.h&gt;
int main(void)
{
puts("Hello, World!");
}
</code>
</program>
</listing>
View Source for program
<program xml:id="c-hello-world-code-lens" interactive="codelens" language="c">
<code>
#include &lt;stdio.h&gt;
int main(void)
{
puts("Hello, World!");
}
</code>
</program>
Listing 3.3.2. An C program, stepable with CodeLens
View Source for listing
<listing xml:id="program-codelens-java">
<caption>A Java program, stepable with CodeLens</caption>
<program xml:id="java-hello-world-code-lens" interactive="codelens" language="java">
<code>
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
</code>
</program>
</listing>
View Source for program
<program xml:id="java-hello-world-code-lens" interactive="codelens" language="java">
<code>
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
</code>
</program>
Listing 3.3.3. A Java program, stepable with CodeLens
Now some moderately more complicated programs to find teh prime numbers less than \(20\text{.}\) We do not vouch for the quality of these, or even their correctness!
View Source for listing
<listing xml:id="sieve-python">
<caption><url href="https://www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes" visual="www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes">Sieve of Eratosthenes</url>, Java</caption>
<program xml:id="sieve-codelens-python" interactive="codelens" language="python">
<code>
def SieveOfEratosthenes(n):
# array of type boolean with True values in it
prime = [True for i in range(n + 1)]
p = 2
while (p * p &lt;= n):
# If it remain unchanged it is prime
if (prime[p] == True):
# updating all the multiples
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
# Print
for p in range(n + 1):
if prime[p]:
print (p,end=" ")
# main
if __name__=='__main__':
n = 20
print ("The prime numbers smaller than or equal to", n,"is")
SieveOfEratosthenes(n)

</code>
</program>
</listing>
View Source for program
<program xml:id="sieve-codelens-python" interactive="codelens" language="python">
<code>
def SieveOfEratosthenes(n):
# array of type boolean with True values in it
prime = [True for i in range(n + 1)]
p = 2
while (p * p &lt;= n):
# If it remain unchanged it is prime
if (prime[p] == True):
# updating all the multiples
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
# Print
for p in range(n + 1):
if prime[p]:
print (p,end=" ")
# main
if __name__=='__main__':
n = 20
print ("The prime numbers smaller than or equal to", n,"is")
SieveOfEratosthenes(n)

</code>
</program>
Listing 3.3.4. Sieve of Eratosthenes
 1 
www.tutorialspoint.com/python-program-for-sieve-of-eratosthenes
, Java
View Source for listing
<listing xml:id="sieve-cpp">
<caption><url href="https://www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range" visual="www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range">Sieve of Eratosthenes</url>, C++</caption>
<program xml:id="sieve-codelens-cpp" interactive="codelens" language="cpp">
<code> 
#include &lt;stdio.h&gt;
const int len = 20;
int main() {
int arr[20] = {0};
for (int i = 2; i &lt; len; i++) {
for (int j = i * i; j &lt; len; j+=i) {
arr[j - 1] = 1;
}
}
for (int i = 1; i &lt; len; i++) {
if (arr[i - 1] == 0)
printf(" %d", i);
}
}

</code>
</program>
</listing>
View Source for program
<program xml:id="sieve-codelens-cpp" interactive="codelens" language="cpp">
<code> 
#include &lt;stdio.h&gt;
const int len = 20;
int main() {
int arr[20] = {0};
for (int i = 2; i &lt; len; i++) {
for (int j = i * i; j &lt; len; j+=i) {
arr[j - 1] = 1;
}
}
for (int i = 1; i &lt; len; i++) {
if (arr[i - 1] == 0)
printf(" %d", i);
}
}

</code>
</program>
Listing 3.3.5. Sieve of Eratosthenes
 2 
www.tutorialspoint.com/cplusplus-program-to-implement-sieve-of-eratosthenes-to-generate-prime-numbers-between-given-range
, C++
View Source for listing
<listing xml:id="sieve-java">
<caption><url href="https://www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java" visual="www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java">Sieve of Eratosthenes</url>, Java</caption>
<program xml:id="sieve-codelens-java" interactive="codelens" language="java">
<code>
public class SievePrimeFactors  {
public static void main(String args[]) {
int num = 20;
boolean[] bool = new boolean[num];
for (int i = 0; i&lt; bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i &lt; Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j &lt; num; j = j+i) {
bool[j] = false;
}
}
}
System.out.println("List of prime numbers: ");
for (int i = 2; i&lt; bool.length; i++) {
if(bool[i]==true) {
System.out.println(i);
}
}
}
}

</code>
</program>
</listing>
View Source for program
<program xml:id="sieve-codelens-java" interactive="codelens" language="java">
<code>
public class SievePrimeFactors  {
public static void main(String args[]) {
int num = 20;
boolean[] bool = new boolean[num];
for (int i = 0; i&lt; bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i &lt; Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j &lt; num; j = j+i) {
bool[j] = false;
}
}
}
System.out.println("List of prime numbers: ");
for (int i = 2; i&lt; bool.length; i++) {
if(bool[i]==true) {
System.out.println(i);
}
}
}
}

</code>
</program>
Listing 3.3.6. Sieve of Eratosthenes
 3 
www.tutorialspoint.com/Sieve-of-Eratosthenes-in-java
, Java