Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%% bloom.m %%%
- m = input('Array size: ');
- n = 0;
- total = input('How many numbers will you be inserting?: ');
- nice_k = (m/total)*log(2);
- sprintf('Recomended hash functions: %d', ceil(nice_k))
- k = input('Number of hash functions: ');
- h = zeros(1, k);
- a = zeros(1, m+1);
- while n<total
- x = input('Number to insert: ');
- h = hash(m,x,k);
- disp(h)
- for i=1:length(h)
- if h(i)==1
- n=n+1;
- break;
- end
- end
- for i=1:length(h)
- a(h(i)) = 1;
- end
- n=n+1;
- end
- %%% hash.m %%%
- function res = hash(m, x, k)
- res = zeros(1,k);
- x_bin = dec2bin(x);
- for i=1:k
- temp = '';
- for j=i:k:length(x_bin)
- temp = strcat(temp, x_bin(j));
- end
- res(i)=mod(bin2dec(temp), m)+1;
- end
- end
- %%% bloomask.m %%%
- while true
- x = input('Enter a number: ');
- h = hash(m, x, k);
- ans=0;
- for i=1:length(h)
- if a(h(i))==0
- sprintf('Not found')
- ans = 1;
- break;
- end
- end
- if ans==0
- sprintf('Found with a false positive rate of %d', (1-exp(-k*n/m))^k)
- end
- end
Add Comment
Please, Sign In to add comment