Advertisement
Guest User

Untitled

a guest
Dec 20th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.89 KB | None | 0 0
  1. import std.algorithm;
  2. import std.conv;
  3. import std.format;
  4. import std.range;
  5. import std.stdio;
  6.  
  7. void main() {
  8.     int[] z = [0,1,0,1,1,1,1,0,0,0,1,0,0,1,1,0,1,0,1,1,1,1];
  9.  
  10.     z.heh.formatPolynom.writeln;
  11. }
  12.  
  13. int[] heh(int[] z) {
  14.     int r = 0;
  15.     auto c = new int[z.length];
  16.     auto b = new int[z.length];
  17.     c[0] = b[0] = 1;
  18.  
  19.     while (r < z.length - 1) {
  20.         r += 1;
  21.        
  22.         int d = 0;
  23.         for (int j = 0; j < r; ++j) {
  24.             d ^= c[j] * z[r-j-1];
  25.         }
  26.  
  27.         auto xb = 0 ~ b[0..$-1];
  28.         if (d == 0) {
  29.             b = xb;
  30.         }
  31.         else {
  32.             int[] t;
  33.             foreach (k1, k2; zip(c, xb)) {
  34.                 t ~= k1 ^ k2;
  35.             }
  36.             b = c;
  37.             c = t;
  38.         }
  39.  
  40.         writefln("r=%s d=%s c=%s b=%s", r, d, c.formatPolynom, b.formatPolynom);
  41.     }
  42.  
  43.     return c;
  44. }
  45.  
  46. string formatPolynom(int[] p) {
  47.     string[] strs;
  48.     foreach (i, k; p) {
  49.         if (i == 0 && k) strs ~= "1";
  50.         if (i > 0 && k) strs ~= "x^" ~ i.to!string;
  51.     }
  52.     return strs.join(" + ");
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement