Deterministic Finite Automaton (DFA) can be used to check whether a number “num” is divisible by “k” or not. If the number is not divisible, remainder can also be obtained using DFA. Lets see how to write C code for Divisibility Algorithm using DFA.

We consider the binary representation of ‘num’ and build a DFA with k states. The DFA has transition function for both 0 and 1. Once the DFA is built, we process ‘num’ over the DFA to get remainder.

## C code for Divisibility Algorithm using DFA

#include <stdio.h> #include <stdlib.h> // Function to build DFA for divisor k void preprocess(int k, int Table[][2]) { int trans0, trans1; // The following loop calculates the two transitions for each state, // starting from state 0 for (int state=0; state<k; ++state) { // Calculate next state for bit 0 trans0 = state<<1; Table[state][0] = (trans0 < k)? trans0: trans0-k; // Calculate next state for bit 1 trans1 = (state<<1) + 1; Table[state][1] = (trans1 < k)? trans1: trans1-k; } } // A recursive utility function that takes a ‘num’ and DFA (transition // table) as input and process ‘num’ bit by bit over DFA void isDivisibleUtil(int num, int* state, int Table[][2]) { // process "num" bit by bit from MSB to LSB if (num != 0) { isDivisibleUtil(num>>1, state, Table); *state = Table[*state][num&1]; } } // The main function that divides ‘num’ by k and returns the remainder int isDivisible (int num, int k) { // Allocate memory for transition table. The table will have k*2 entries int (*Table)[2] = (int (*)[2])malloc(k*sizeof(*Table)); // Fill the transition table preprocess(k, Table); // Process ‘num’ over DFA and get the remainder int state = 0; isDivisibleUtil(num, &state, Table); // Note that the final value of state is the remainder return state; } // Driver program to test above functions int main() { int num = 47; // Number to be divided int k = 5; // Divisor int remainder = isDivisible (num, k); if (remainder == 0) printf("Divisible\n"); else printf("Not Divisible: Remainder is %d\n", remainder); return 0; }

**Output:**

Not Divisible: Remainder is 2

DFA based division can be useful if we have a binary stream as input and we want to check for divisibility of the decimal value of stream at any time.

Let us know if you face any issues in understanding code or compiling a code.

nice, thank you so much ;)

Good day. Very nice web site!! Man .. Excellent .. Wonderful .. I will bookmark your site and take the feeds also…I am glad to find a lot of useful info right here in the article. Thanks for sharing..

this tutorial is very helfpul but all the templates i got has lock the background Can u give me the site where i can fully use all the features of the templates please

A lot of the commments on this weblog demonstrate that there is full ignorance about these items

magnificent post, very informative. I’m wondering why the other specialists of this sector don’t realize this. You should proceed your writing. I’m sure, you have a huge readers’ base already!

very good and cool,thank you for your sharing. You have to go places http://www.burberryscarfbigsale.com/

Wonderful post! We will be linking to this particularly great article on our site. Keep up the great writing.

Thank you very much. This really helped me with my work. I appreciate your help. Thanks a lot. welcome to my site http://www.hermesbeltscheapsale.com/

replica hermes belts red and black http://www.hermesbeltscheapsale.com/replica-hermes-belts-red-and-black-45139-p-583.html

Good day! Do you know if they make any plugins to safeguard against hackers?

I’m kinda paranoid about losing everything I’ve worked hard

on. Any suggestions?

WOW just wyat I was looking for. Camee here by searching for adfly herobrine mod