
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
DFA based division
Deterministic Finite Automaton(DFA) is used to check whether a number is divisible by another number k or not. If it is not divisible, then this algorithm will also find the remainder.
For the DFA based division, at first, we have to find the transition table of the DFA, using that table, we can easily find the answer. In the DFA, each state has only two transition 0 and 1.
Input and Output
Input: The number: 50 and the divisor 3 Output: 50 is not divisible by 3 and remainder is: 2
Algorithm
dfaDivision(num, k)
Input: A number num, and divisor k.
Output: Check divisibility and the remainder.
Begin create transition table of size k * 2 //2 for transition 0 and 1 state = 0 checkState(num, state, table) return state End
checkState(num, state, table)
Input: A number num, state, and the transition table.
Output: Update the state after performing division.
Begin if num ≠ 0, then tempNum := right shift number for i bit checkState(tempNum, state, table) index := number AND 1 //perform logical and with number and 1 state := table[state][index] End
Example
#include <iostream> using namespace std; void makeTransTable(int n, int transTable[][2]) { int zeroTrans, oneTrans; for (int state=0; state<n; ++state) { zeroTrans = state<<1; //next state for bit 0 transTable[state][0] = (zeroTrans < n)? zeroTrans: zeroTrans-n; oneTrans = (state<<1) + 1; //next state for bit 1 transTable[state][1] = (oneTrans < n)? oneTrans: oneTrans-n; } } void checkState(int num, int &state, int Table[][2]) { if (num != 0) { //shift number from right to left until 0 checkState(num>>1, state, Table); state = Table[state][num&1]; } } int isDivisible (int num, int k) { int table[k][2]; //create transition table makeTransTable(k, table); //fill the table int state = 0; //initially control in 0 state checkState(num, state, table); return state; //final and initial state must be same } int main() { int num; int k; cout << "Enter Number, and Divisor: "; cin >> num>> k; int rem = isDivisible (num, k); if (rem == 0) cout<<num<<" is divisible by "<<k; else cout<<num<<" is not divisible by "<<k<<" and remainder is: " << rem; }
Output
Enter Number, and Divisor: 50 3 50 is not divisible by 3 and remainder is: 2
- Related Articles
- DFA based division in C++?
- C Program to construct a DFA which accepts L = {aN | N ≥ 1}
- C++ Program to compute division upto n decimal places
- What is a distance-based outlier?\n
- What is Minimization of DFA?
- Explain Union process in DFA
- Explain the concatenation process in DFA
- Explain the complementation process in DFA
- What is Deterministic Finite Automata (DFA)?
- How to Alternate Row Colour Based on Group in Excel?\n
- What is the difference between DFA and NFA?
- Construct DFA for strings not ending with "THE"
- Explain the cross product method process in DFA
- What is minimizing of DFA in compiler design?
- Complete the following flow chart based on ecosystem and its components."\n

Advertisements