Check if a number is Fibonacci

A Fibonacci number is one which appears in the Fibonacci Sequence. To check if a given number n occurs in the Fibonacci Sequence or not we only need to check if one or both of (5*n^2 + 4) or (5*n^2 – 4) is a perfect square or not (source: wiki). Python code for the same: ...

Pascal Triangle in C

Pascal Triangle in C
The following C code generates on standard output a Pascal Triangle as specified on this wiki page. Specifically output in following format is generated: C /* The following code is used to print the Pascal triangle for as many rows as input by the user. */ #include<stdio.h> // begin the main function for this program ...

Code Jam Cookie Clicker Alpha

This is a solution to the Code Jam 2014 Qualification Round problem B, Cookie Clicker Alpha. (Problem Description Link) Python __author__ = 'Coddicted' def read_case(inf): return inf.readline().split(' ') def write_case(f, i, res): f.write('Case #%d: '%i) f.write('%.7f'%res) f.write('\n') def main(): with open('data_files/B-large-practice.in', 'r') as inf, open('data_files/B-large-practice.out', 'w') as of: t = int(inf.readline()) case_cnt = 1 while ...

Count Triangles in a Graph 1

The following code implementation counts the number of triangles present in a graph G. For graph representation JUNG API is used but can be extended/ changed to use any other notation by simple means. Java import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import edu.uci.ics.jung.graph.Graph; public class TriangleFinder { private Graph<Integer, String> g; /** * @return The ...

Dijkstra’s Algorithm : Finding all possible shortest paths between two vertices in a graph 4

Dijkstra's Algorithm : Finding all possible shortest paths between two vertices in a graph
The following code implements the Dijkstra’s Shortest Path Algorithm and further extends is to get all possible shortest paths between two vertices. For graph representation we make use of JUNG API, its usage, however, is primarily in visualizing the graph and can be extended easily for any other representation as well. Random Graph Generator code ...

Code for Random Graph Generation G(n,p) 1

Code for Random Graph Generation G(n,p)
Following is the Java code for generating a Random Graph G(n, p) where ‘n’ is the number of nodes/ vertices in the graph and ‘p’ is the probability that any edge will form or not. The code makes use of JUNG API for graph visualization and creation. More About JUNG… Java import java.awt.Dimension; import javax.swing.JFrame; ...

Matlab code to create k-connected Harary Graph 4

Matlab code to create k-connected Harary Graph
The following is a MATLAB script to create a k-connected Harary Graph of n-nodes. Clearly the inputs required are n (no of nodes) and k (degree of each node). Also, while the code is a MATLAB script the basic technique to generate the adjacency matrix of the graph can be easily adopted to other languages ...

DDA Line

Description:              This program implements the DDA algorithm of line drawing.   Primary Input:        Two end point coordinates of the line. Primary Output:       A line drawn according to the given end points. Platform Used:         Turbo C++ version 3.0, Borland International Inc.   /*This program generates a line according to DDA algorithm for given end points. */ #include #include #include #define ROUND(a) ...

net.cpp

Description:          This program is Menu driven program for Creating/deleting inodes and connection in the Graphs.   Platform Used:      g++ (GNU C++, a freely redistributable C++ compiler).   /*net.cpp*/ #include #include "help.h" using namespace std; int main() { graph g; int x,choice,l, y,m,n,d,c;; static int count; do { choice=g.displaymenu(); switch(choice) { case 1:x=++count; g.adb(g.p,x); cout

Adding rows and columns in JTable dynamically 5

Adding rows and columns in JTable dynamically
Java Source code to add Row and Column in JTable dynamically package com.example; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Vector; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.WindowConstants; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; public class JTableSample { private JFrame mainFrame ; private JTable ...