Created
February 7, 2012 03:42
-
-
Save bjacobso/1757020 to your computer and use it in GitHub Desktop.
Farey
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
// File: Farey.cxx | |
// Written by Matt Jacobson (Matthew.G.Jacobson@Colorado.edu) | |
// Date: 02/03/12 | |
// This program calculates Farey numbers | |
// | |
// used http://www.cplusplus.com/doc/tutorial/arrays/ for reference | |
// | |
// Directives: | |
#include <cmath> | |
#include <cstdlib> | |
#include <iostream> | |
#include <graphics.h> | |
using namespace std; | |
int x; // Screen Size | |
int i; // X index of gcd | |
int j; // Y index of gcd | |
int test; // Returns 1 if comprime | |
int counter; // Counts Visible Points | |
// Prototypes: | |
//---------------------------------------------------------- | |
int gcd(int a, int b); | |
int calc_vis_pts(int M, int N); | |
template<typename D> | |
void printarray(const D *array); | |
//---------------------------------------------------------- | |
// Function definitions: | |
//---------------------------------------------------------- | |
int main( ) | |
{ | |
int m; // X-Coordinate of Center | |
int n; // Y-Coordinate of Center | |
cin >> x; | |
int stored[x-1][x-1]; | |
for(m=1; m<=x; m++) | |
{ | |
for(n=1; n<=x; n++) | |
{ | |
calc_vis_pts(m,n); | |
stored[x-n][m-1]=counter; | |
} | |
} | |
printarray(stored, x); | |
} | |
//---------------------------------------------------------- | |
//---------------------------------------------------------- | |
int gcd(int a, int b) | |
{ | |
if(b==0) | |
{ | |
return a; | |
} | |
else | |
{ | |
return gcd(b, a%b); | |
} | |
} | |
//---------------------------------------------------------- | |
//---------------------------------------------------------- | |
int calc_vis_pts(int M, int N) | |
{ | |
int counter = 0; | |
for(i=0; i<=x-1; i++) | |
{ | |
for(j=0; j<=x-1; j++) | |
{ | |
test = gcd(M-i,N-j); | |
if(test==1) | |
{ | |
counter++; | |
} | |
} | |
} | |
return counter; | |
} | |
//---------------------------------------------------------- | |
//---------------------------------------------------------- | |
void printarray(int array[], int x) | |
{ | |
// | |
// TODO: if you want to reference x I think you have to pass it into the function | |
// TODO: also the all caps is usually for constants, these should be lower case variable names | |
// | |
int ROW = x-1; | |
int COL = x-1; | |
for(int rowCnt = 0; rowCnt < ROW; rowCnt++) | |
{ | |
for(int colCnt = 0; colCnt < COL; colCnt++) | |
{ | |
cout<< array[rowCnt][colCnt]; | |
} | |
cout << endl; | |
} | |
} | |
//---------------------------------------------------------- |
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
# | |
# $ python farey.py 30 | |
# | |
import sys | |
# this prints the things you pass into python | |
print sys.argv | |
# print the number you pass into the program | |
print sys.argv[1] | |
# get the number and convert it from a string to an int | |
x = int(sys.argv[1]) | |
# generate the matrix | |
stored = [ [ 0 for i in range(x) ] for j in range(x) ] | |
def print_array(array): | |
for line in array: | |
print line | |
# int calc_vis_pts(int M, int N) | |
# { | |
# int counter = 0; | |
# | |
# for(i=0; i<=x-1; i++) | |
# { | |
# for(j=0; j<=x-1; j++) | |
# { | |
# test = gcd(M-i,N-j); | |
# if(test==1) | |
# { | |
# counter++; | |
# } | |
# } | |
# } | |
# return counter; | |
# } | |
def calc_vis_pts(m, n): | |
counter = 0 | |
for i in range(0, x): | |
for j in range(0, x): | |
test = gcd(m-i, n-j) | |
if test == 1: | |
counter += 1 | |
return counter | |
# int gcd(int a, int b) | |
# { | |
# if(b==0) | |
# { | |
# return a; | |
# } | |
# else | |
# { | |
# return gcd(b, a%b); | |
# } | |
# } | |
def gcd(a, b): | |
if b == 0: | |
return a | |
else: | |
return gcd(b, a%b) | |
# for(m=1; m<=x; m++) | |
# { | |
# for(n=1; n<=x; n++) | |
# { | |
# int counter = calc_vis_pts(m,n); | |
# stored[x-n][m-1]=counter; | |
# } | |
# } | |
for m in range(1,x+1): | |
for n in range(1,x+1): | |
stored[x-n][m-1] = calc_vis_pts(m,n) | |
print_array(stored) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment