Created
December 10, 2016 01:52
-
-
Save hygull/33f4c43eb5d380a43863db850e78244c to your computer and use it in GitHub Desktop.
Prime numbers, displaying primes(1-100) in tabular form created by hygull - https://repl.it/EmWQ/10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Date of creation : 10/12/2016. | |
Aim of program : To display primes(between 1-100) in tabular form. | |
Coded by : Rishikesh Agrawani. | |
*/ | |
package main | |
import "fmt" | |
func main() { | |
//We know 1 is not prime but here our intention is tabluar form so that is why I am executing for from 1 to onwards. | |
//Here(In main) we are not focusing on optimization and time complexity etc. | |
count:=0 | |
for i := 1; i < 11; i++ { | |
n := i | |
for j := 1; j <11; j++ { | |
if j != 1{ | |
n += 10 //In first line 1->11->21->31->41...91. In 2nd line 2->12->22->32...92, and so on. | |
}else{ | |
n = i //In first column 1->2,3->4->...10 will be there so assign i's value to n directly. | |
} | |
if isPrime(n) { //isPrime() function is defined after main | |
fmt.Print(n," \t") | |
count+=1 | |
}else{ | |
fmt.Print("-"," \t") | |
} | |
} | |
fmt.Println() //To go next line | |
} | |
fmt.Println("\nTotal number of primes between (1-100): ",count) | |
} | |
//A function that checks whether the accepted argument is Prime or not, and returns true if it is otherwise false | |
func isPrime(n int) (bool){ | |
isNumPrime:=true | |
if n==1{ //1 is not a prime so return directly from here | |
return false | |
} | |
for k:=2;k<n;k++{ | |
if n%k == 0 { //If any number divides except 1 and itself | |
isNumPrime = false //then number is not prime | |
break //For best performance as we know number is not prime so there is no need to iterate further | |
} | |
} | |
return isNumPrime | |
} | |
/*OUTPUT:- | |
- 11 - 31 41 - 61 71 - - | |
2 - - - - - - - - - | |
3 13 23 - 43 53 - 73 83 - | |
- - - - - - - - - - | |
5 - - - - - - - - - | |
- - - - - - - - - - | |
7 17 - 37 47 - 67 - - 97 | |
- - - - - - - - - - | |
- 19 29 - - 59 - 79 89 - | |
- - - - - - - - - - | |
Total number of primes (between 1-100): 25 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment