Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a twin prime number code #559

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions Algorithms/C++/dfs.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
class Graph
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
int main()
{
public:
map<int, bool> visited;
map<int, list<int>> adj;
void addEdge(int v, int w);
void DFS(int v);
};

void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
}

void Graph::DFS(int v)
{
visited[v] = true;
cout << v << " ";
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
}

int m;
cout <<"Enter no of vertices: ";
cin >> n;
cout <<"Enter no of edges: ";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from: ";
cin >>v;
cout <<"DFS ORDER OF VISITED VERTICES: ";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n; j>=1; j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
return 0;
}
46 changes: 46 additions & 0 deletions Algorithms/C/twinPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//twin Primes
#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
int main()
{

int num;
int prime;
printf("Enter a positive Integer: ");
scanf("%d", &num); //taking input

while (num < 0)
{
printf("Invalid input!\nEnter Positive Integer Again: ");
scanf("%d", &num);
}

for (int i = 3; i < num; i++)
{
prime = 0;
for (int j = 3; j < i; j++)
{
if (i % j == 0 || (i + 2) % j == 0)
{
prime = 1;
}
}

if (prime == 0)
{
//printf("Twin prime numbers between 2 and %d are ", num);
printf("(%d,%d)", i, i + 2);
printf("\n");
}
}








return 0;
}