1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
use crate::arithmetic::conversion::*;
use crate::arithmetic::inlineops::*;

use crate::arithmetic::sign::Sign;
use crate::arithmetic::sliceops::*;
use std::cmp::Ordering;

use crate::traits::NumberTheory;

/// Arbitrary precision integer (Z)
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Mpz {
    pub(crate) sign: Sign,
    pub(crate) limbs: Vec<u64>,
}

impl Mpz {
    /**

    ```
     use number_theory::Mpz;  // includes arbitrary-precision arithmetic
     use number_theory::Sign; // allows sign
     use number_theory::NumberTheory; // includes methods from NumberTheory trait

     let bignum = Mpz::new(Sign::Positive, vec![5,5]);

     let fortyfour = Mpz::from_u128(44u128);

     let fortyfour_neg = Mpz::from_i128(-44i128);

     let twopow = Mpz::from_u64(128);


     assert_eq!("92233720368547758085", bignum.to_string());
     assert_eq!("44", fortyfour.to_string());
     assert_eq!("-44", fortyfour_neg.to_string());
     assert_eq!("128", twopow.to_string());

    ```

    */
    
      /// Returns a positive x from a big-endian vector representation 
    pub fn u_new(limbs: Vec<u64>) -> Self {
        Mpz::from_slice(Sign::Positive, &limbs[..])
    }
      /// Returns a x from a big-endian vector representation and a sign 
    pub fn new(sign: Sign, limbs: Vec<u64>) -> Self {
        Mpz::from_slice(sign, &limbs[..])
    }
     /// Returns a positive x from a big-endian vector, this function does not eliminate extraneous leading zeros 
    pub fn unchecked_u_new(limbs: Vec<u64>) -> Self {
        Mpz {
            sign: Sign::Positive,
            limbs,
        }
    }

  /// Returns a  x from a big-endian vector and sign, this function does not eliminate extraneous leading zeros 
    pub fn unchecked_new(sign: Sign, limbs: Vec<u64>) -> Self {
        Mpz { sign, limbs }
    }
/// Returns x from a big_endian slice and sign
    pub fn from_slice(sign: Sign, x: &[u64]) -> Self {
        let mut limbs = vec![];
        limbs.extend_from_slice(&x[..sig_pos(x)]);
        if limbs.is_empty() {
            limbs.push(0u64)
        }
        Mpz { sign, limbs }
    }
/// Return x from u128
    pub fn from_u128(x: u128) -> Self {
        let (x_lo, x_hi) = split(x);
        if x_hi == 0 {
            return Mpz::unchecked_new(Sign::Positive, vec![x_lo]);
        }
        Mpz::unchecked_new(Sign::Positive, vec![x_lo, x_hi])
    }
/// Return x from i128
    pub fn from_i128(x: i128) -> Self {
        if x < 0i128 {
            let (x_lo, x_hi) = split(x.abs() as u128);
            if x_hi == 0 {
                return Mpz::unchecked_new(Sign::Negative, vec![x_lo]);
            }
            Mpz::unchecked_new(Sign::Negative, vec![x_lo, x_hi])
        } else {
            Mpz::from_u128(x as u128)
        }
    }
/// Return x from u64
    pub fn from_u64(x: u64) -> Self {
        Mpz::unchecked_new(Sign::Positive, vec![x])
    }
/// Return x from i64
    pub fn from_i64(x: i64) -> Self {
        if x < 0i64 {
            return Mpz::unchecked_new(Sign::Negative, vec![x.abs() as u64]);
        }
        Mpz::unchecked_new(Sign::Positive, vec![x as u64])
    }
     /// Converts to 64-bit unsigned integer if it can fit
    pub fn to_u64(&self) -> Option<u64> {
        match self.len() {
            0 => Some(0u64),
            1 => Some(self.limbs[0]),
            _ => None,
        }
    }
    
    /// Convert to 64-bit signed integer
    pub fn to_i64(&self) -> Option<i64>{
       let mut flag : i64 = 1;
       if !self.is_positive(){
         flag = -1;
       }
        match self.len() {
            0 => Some(0i64),
            1 => {
             if self.limbs[0] > i64::MAX as u64{
              return None
             }  
             return Some((self.limbs[0] as i64)*flag)
            },
            _ => None,
        }
    }
    /// Converts to 32-bit unsigned integer if it can fit 
    pub fn to_u32(&self) ->  Option<u32>{
       match self.len() {
            0 => Some(0u32),
            1 => {
                   if self.limbs[0] > u32::MAX as u64 {
                      return None
                   } 
                   return Some(self.limbs[0] as u32);
                 }
            _ => None,
        }
    }
     /// Converts to 128-bit unsigned integer if it can fit
    pub fn to_u128(&self) -> Option<u128> {
        match self.len() {
            0 => Some(0u128),
            1 => Some(self.limbs[0] as u128),
            2 => Some(fuse(self.limbs[1], self.limbs[0])),
            _ => None,
        }
    }
    /// Returns the vector representation of n
    pub fn to_vector(&self) -> Vec<u64> {
        self.limbs.clone()
    }
   
     /// Set an integer between 2^(n-1);2^n
    pub fn rand(len: usize) -> Self {
      let mut k = len/64;
      if len%64 != 0{
        k+=1;
      }
      let mut interim = (0..k).map(|_| u64::rng()).collect::<Vec<u64>>();
      
      interim[k-1] =  interim[k-1]&((1<<(len%64))-1);
      let mut result = Mpz::unchecked_new(Sign::Positive, interim);
      result.set_bit(len-1);
      result
      
      
    }
    
    
    /// Creates a integer from limbs generated by a function 
    pub fn set_limbs(len: usize, gen: fn() -> u64) -> Self {
        let interim = (0..len).map(|_| gen()).collect::<Vec<u64>>();
        Mpz::unchecked_new(Sign::Positive, interim)
    }
    /// Converts n to a radix-10 string
    pub fn to_string(&self) -> String {
        to_string(self.sign.clone(), self.limbs.clone())
    }
    // temporary placeholder function to flip negative zeros
    pub(crate) fn fix_zero(&mut self) {
        if self.len() == 0 {
            self.limbs.push(0u64)
        }
        if self.len() == 1 && self.limbs[0] == 0 && self.sign == Sign::Negative {
            self.sign = Sign::Positive;
        }
    }
    /**
      Returns the polynomial representation of self in the form of the coefficient vector. Here we see that 50620 to radix-127 is 3x^2 + 17x + 74.
      Accepts all radix in the interval 0;2^64-1.

    ```
    use number_theory::Mpz;

     let value = Mpz::from_u64(50620);
     assert_eq!(value.to_radix_vec(127),vec![74,17,3])
    ```
    */

    pub fn to_radix_vec(&self, radix: u64) -> Vec<u64> {
        let mut k = vec![];
        let mut x = self.clone().limbs;
        loop {
            let idx = sig_pos(&x[..]);

            if idx == 0 {
                break;
            }

            k.push(div_slice(&mut x[..idx], radix));
        }
        k.push(x[0] % radix);
        remove_lead_zeros(&mut k);
        k
    }

    /**
      Conversion from radix-10^n string.

    ```
    use number_theory::Mpz;
     let num = "-3141592653589793238462643383279502884197169399375105820974944592307816406286".to_string();
     let machine = Mpz::from_string(&num).unwrap();

     assert_eq!(machine.to_string(),num)
    ```
    */

    pub fn from_string(x: &str) -> Option<Self> {
        if x.is_empty() {
            return None;
        }

        let ch = x.chars().next().unwrap();
        let mut sign = Sign::Positive;
        let mut k = x;
        if ch == '-' {
            sign = Sign::Negative;
            let mut chars = x.chars();
            chars.next();
            k = chars.as_str();
        }

        if ch == '+' {
            let mut chars = x.chars();
            chars.next();
            k = chars.as_str();
        }

        from_string(k).map(|y| Mpz::unchecked_new(sign, y))
    }
    /// Returns 0
    pub fn zero() -> Self {
        Mpz::unchecked_new(Sign::Positive, vec![0])
    }
    /// Returns positive 1
    pub fn one() -> Self {
        Mpz::unchecked_new(Sign::Positive, vec![1])
    }
    /// Returns positive 2
    pub fn two() -> Self{
       Mpz::unchecked_new(Sign::Positive, vec![2])
    }
    /// Additive inverse of n, evaluated in-place 
    pub fn neg(&mut self) {
        self.sign = self.sign.neg();
    }
     /// Returns the absolute value of n  
    pub fn abs(&self) -> Self {
        Self::new(Sign::Positive, self.limbs.clone())
    }
    ///Checks if n == 1 or n == -1
    pub fn is_one(&self) -> bool {
        if self.len() == 1 && self.limbs[0] == 1 {
            return true;
        }
        false
    }
    
    /// Checks if n == 0
    pub fn is_zero(&self) -> bool {
       if self.len() == 0{
         return true
       }
       if self.len() == 1 && self.limbs[0] == 1{
         return true
       }
       return false
    }
    /// Checks if n >= 0
    pub fn is_positive(&self) -> bool {
        self.sign == Sign::Positive
    }
     /// Checks if n in 2Z
    pub fn is_even(&self) -> bool {
        self.limbs[0] & 1 == 0
    }
    /// Checks if n in 2^Z
    pub fn is_power_of_two(&self) -> bool {
        if self.len() < 2 {
            return self.to_u128().unwrap().is_power_of_two();
        }
        if !self.lead_digit().is_power_of_two() {
            return false;
        }
        for i in self.limbs[..self.len() - 1].iter() {
            if *i != 0 {
                return false;
            }
        }
        return true;
    }

    pub(crate) fn is_fermat(&self) -> bool {
        if self.limbs[0] != 1 {
            return false;
        }

        let lead = self.limbs[..].last().unwrap();

        let mut flag = 0u64;
        for i in 0..64 {
            if *lead == 1u64 << i {
                flag = 1; // set flag
                break; // end loop
            }
        }

        if flag == 0 {
            return false;
        } // if the flag is not set return false

        for i in self.limbs[1..self.len() - 1].iter() {
            if *i != 0u64 {
                return false;
            }
        }
        true
    }

    pub(crate) fn is_mersenne(&self) -> Option<u64> {
        let mut flag = 0u64;
        let mut start = 1u64;
        let lead = self.limbs.last().unwrap();
        for i in 0..64 {
            if *lead == start {
                flag = i;
                break;
            }
            start = (start << 1) + 1;
        }
        if flag == 0 {
            return None;
        }
        for i in self.limbs[..self.len() - 2].iter() {
            if *i != u64::MAX {
                return None;
            }
        }

        Some(flag + 1 + 64u64 * (self.len() - 1) as u64)
    }
    /// Sets the bit at 2^k to 1, if it is already 1 then this does not change
    pub fn set_bit(&mut self, k: usize) {
        self.limbs[k / 64usize] |= 1 << (k % 64)
    }
    /// Flips the bit at 2^k
    pub fn flip_bit(&mut self, k: usize){
         self.limbs[k / 64usize] ^= 1 << (k % 64)
    }
   /// Check if the bit in the k-place is set  
    pub fn check_bit(&self, index: usize) -> bool {
        self.limbs[index / 64usize] & (1 << (index % 64)) > 0
    }
   /// Change the sign   
    pub fn set_sign(&mut self, sign: Sign) {
        self.sign = sign
    }
    /// Returns the number of 64-bit machine words used in this representation
    pub fn len(&self) -> usize {
        self.limbs.len()
    }
    /// Returns the lead digit in 2^64 representation
    pub fn lead_digit(&self) -> u64 {
        *self.limbs[..].last().unwrap()
    }
   /// Returns k such that  d*2^k = x
    pub fn trailing_zeros(&self) -> u64 {
        // Trailing zeros
        let mut idx: u64 = 0;

        for i in self.limbs.iter() {
            if i == &0u64 {
                idx += 1;
            } else {
                break;
            }
        }
        if idx == self.len() as u64 {
            64u64 * idx
        } else {
            self.limbs[idx as usize].trailing_zeros() as u64 + 64u64 * idx
        }
    }
    /// Returns the number of extraneous zeros in bit representation
    pub fn leading_zeros(&self) -> u64 {
        let mut idx: u64 = 0;

        for i in self.limbs.iter().rev() {
            if i == &0u64 {
                idx += 1;
            } else {
                break;
            }
        }
        if idx == self.len() as u64 {
            64u64 * idx
        } else {
            self.limbs[self.len() - 1 - idx as usize].leading_zeros() as u64 + 64u64 * idx
        }
    }
  /// Returns the number of bits used to represent x 
    pub fn bit_length(&self) -> u64 {
        64u64 * self.len() as u64 - self.leading_zeros()
    }
   /// Removes extraneous zeros 
    pub fn normalize(&mut self) {
        remove_lead_zeros(&mut self.limbs);
        if self.len() == 0 {
            self.limbs.push(0u64)
        };
    }

    /*
    Equality Operations

    */
  /// Compares the magnitude of x and y, ignoring sign
    pub fn u_cmp(&self, other: &Self) -> Ordering {
        cmp_slice(&self.limbs[..], &other.limbs[..])
    }
   /// Checks if x mod n === c 
    pub fn congruence_u64(&self, n: u64, c: u64) -> bool {
        let mut interim = mod_slice(&self.limbs[..], n);
        if self.sign == Sign::Negative {
            interim = n - interim;
        }
        interim == c
    }

    pub(crate) fn add_modinv(&self, n: &Self) -> Self {
        // additive modular inverse
        let mut k = n.clone();
        let mut selfie = self.clone();

        if self.u_cmp(n) == Ordering::Greater {
            selfie = selfie.ref_euclidean(n).1;
        }

        sub_slice(&mut k.limbs, &selfie.limbs);
        k.normalize();
        k.sign = Sign::Positive;
        k
    }

    /**

       ```
             use number_theory::Mpz;
         let mut one = Mpz::one();

         one.successor();  //Applies the successor function ignoring sign

         assert_eq!("2", one.to_string())

       ```


    */

    pub fn successor(&mut self) {
        if self.len() == 0 {
            self.limbs.push(1)
        }
        let mut carry = 1u8;
        for i in self.limbs.iter_mut() {
            carry = adc(carry, *i, 0, i);
            if carry == 0 {
                break;
            }
        }
        if carry > 0u8 {
            self.limbs.push(1u64)
        }
    }
     /// Increments by n amount
    pub fn inc_by(&mut self, n: u64){
      if self.len() == 0 {
            self.limbs.push(n)
        }
        
        add_slice(&mut self.limbs[..],&[n]);
    }
    
    /// Inverse successor function. Subtracts 1 ignoring sign
    pub fn inv_successor(&mut self) {
        if self.len() == 0 {
            panic!("Value already zero")
        }
        let mut carry = 1u8;
        for i in self.limbs.iter_mut() {
            carry = sbb(carry, *i, 0, i);
            if carry == 0 {
                break;
            }
        }
        if carry > 0u8 {
            panic!("Value already zero")
        }
    }

    /*
      Precursor NT functions, unsigned
    */

    pub(crate) fn u_quadratic_residue(&self, n: &Self) -> Self {
        self.ref_product(self).ref_euclidean(n).1
    }

    pub(crate) fn u_mul_mod(&self, other: &Self, n: &Self) -> Self {
        self.ref_product(other).ref_euclidean(n).1
    }

    pub(crate) fn u_mod_pow(&self, y: &Self, modulo: &Self) -> Self {
        if modulo == &Mpz::zero() {
            return Mpz::zero();
        }

        let mut z = Mpz::one();
        let mut base = self.clone().ref_euclidean(modulo).1;
        let one = Mpz::one();

        let mut pow = y.clone();

        if pow == Mpz::zero(){
            return z;
        }

        if pow == Mpz::one() {
            return base;
        }

        while pow.u_cmp(&one) == Ordering::Greater {
            if pow.len() == 0 {
                break;
            }

            if pow.is_even() {
                base = base.u_quadratic_residue(modulo);
                pow.mut_shr(1);
                remove_lead_zeros(&mut pow.limbs);
            } else {
                z = base.u_mul_mod(&z, modulo);
                remove_lead_zeros(&mut base.limbs);
                base = base.u_quadratic_residue(modulo);

                sub_slice(&mut pow.limbs[..], &one.limbs[..]);
                pow.mut_shr(1);
                remove_lead_zeros(&mut pow.limbs);
            }
        }
         base.u_mul_mod(&z, modulo)
    }

    /// Returns self^x
    pub fn pow(&self, x: u64) -> Self {
        let mut z = Mpz::one();
        let mut base = self.clone();
        let mut pow = x;

        if pow == 0 {
            return z;
        }

        while pow > 1 {
            if pow % 2 == 0 {
                base = base.ref_product(&base);
                pow >>= 1;
            } else {
                z = base.ref_product(&z);
                base = base.ref_product(&base);
                pow = (pow - 1) >> 1;
            }
        }

        base.ref_product(&z)
    }

    pub(crate) fn word_div(&self, x: u64) -> (Self, u64) {
        let mut quotient = self.clone();
        let remainder = div_slice(&mut quotient.limbs[..], x);
        (quotient, remainder)
    }

    fn delta(&self, other: &Self) -> Self {
        if self.u_cmp(other) == Ordering::Greater {
            let mut k = self.clone();
            sub_slice(&mut k.limbs[..], &other.limbs[..]);
            return k;
        }
        if self.u_cmp(other) == Ordering::Less {
            let mut k = other.clone();
            sub_slice(&mut k.limbs[..], &self.limbs[..]);
            k
        } else {
            Mpz::zero()
        }
    }

    fn mod_sqr_1(&self, n: &Self) -> Self {
        let mut k = self.ref_product(self);
        k.successor();
        k.ref_euclidean(n).1
    }

    fn gcd(&self, other: &Self) -> Self {
        let mut a = self.clone();
        let mut b = other.clone();

        while b != Mpz::zero() {
            let t = b.clone();

            b = a.ref_euclidean(&b).1;

            b.normalize();
            a = t;
        }
        a
    }

    pub(crate) fn rho_mpz(&self) -> Self {
        let mut x = Mpz::two();
        let mut y = Mpz::two();
        let mut d = Mpz::one();
        loop {
            while d == Mpz::one() {
                x = x.mod_sqr_1(self);
                y = y.mod_sqr_1(self).mod_sqr_1(self).ref_euclidean(self).1;
                d = x.delta(&y).gcd(self);
            }

            if d.is_prime() {
                return d;
            }
            x = Mpz::from_u64(u64::rng());
            y = x.clone();
            d = Mpz::one();
        }
    }

    /// Aproximation of the natural logarithm ln(x)
    pub fn ln(&self) -> f64 {
        let mut iter_sqrt = 1u64;
        let mut n = self.sqrt().0;
        while n.len() > 1 {
            n = n.sqrt().0;
            iter_sqrt += 1;
        }

        (n.to_u64().unwrap() as f64).ln() * 2f64.powf(iter_sqrt as f64)
    }
    /// Approximation of the base-2 logarithm
    pub fn log2(&self) -> f64 {
        self.ln() * std::f64::consts::LOG2_E
    }
    /// Approximation of the base-10 logarithm
    pub fn log10(&self) -> f64 {
        self.ln() * 0.43429448190325176
    }
    /// Approximation of the base-k logarithm, where k is a Real.
    pub fn log(&self, log: f64) -> f64 {
        self.ln() * log.ln().recip()
    }
    /// Iterated logarithm
    pub fn iter_log(&self, log: f64) -> u8 {
        let mut first_log = self.log(log);
        let mut count = 1u8;
        // 1.444667861009766

        while first_log > 1.0 {
            first_log = first_log.log(log);
            count += 1
        }
        count
    }
    
    /// Finite ring variant of Extended Euclidean algorithm, see trait definition of extended gcd
    pub fn eea(&self, other: &Self) -> (Self, Self, Self) {
    
    let (gcd, bezout_1, bezout_2) = self.extended_gcd(other);
    
    return (gcd, bezout_1.residue(other), bezout_2.residue(self))
     
    }

    /**
    Sequential Interval Residue Product, a generalization of the k-factorials
    
      ```

         // see the sirp crate for greater extension of this functionality
          use number_theory::Mpz;
          
         let factorial_100 = Mpz::sirp(1,100,1,0);
         
         // Even. Odd double factorial would be sirp(1,100,2,1)
         let doublefact_100 = Mpz::sirp(1,100,2,0);
         assert_eq!(
         "3424322470251197624824643289520818597\
          5118675053719198827915654463488000000000000",doublefact_100.to_string())
      ```
    */

    pub fn sirp(infimum: u64, supremum: u64, modulo: u64, residue: u64) -> Self {
        let mut sirp = Mpz::one();
        let mut acc = 1u64; // accumulator for factors

        for i in infimum..supremum + 1 {
            // inclusive range

            if i % modulo == residue {
                // if i is of the residue class modulo n then multiply   If you set residue as stop mod n then you get the k-factorials

                if i >= 4294967296 {
                    acc = i
                }
                if acc < 4294967296 {
                    acc *= i
                }
                if acc >= 4294967296 {
                    let carry = scale_slice(&mut sirp.limbs[..], acc);

                    if carry > 0 {
                        sirp.limbs.push(carry)
                    }
                    acc = 1u64;
                } // end if
            } // else
        }

        let carry = scale_slice(&mut sirp.limbs[..], acc);
        if carry > 0 {
            sirp.limbs.push(carry)
        }
        sirp
    }
    /**
        Conditional Interval Product computes the product of integers satisfying an unary function. In this example the unary function is the
         primality function. In practice it can be any function that borrows a u64 and returns a boolean. 

      ```

          use number_theory::Mpz;
          use number_theory::NumberTheory;

         let primorial_25 = Mpz::cip(1,100,u64::is_prime);


      ```
    */

    // conditional interval product
    pub fn cip(infimum: u64, supremum: u64, cond: fn(&u64) -> bool) -> Self {
        let mut cip = Mpz::one();
        let mut acc = 1u64; // accumulator for factors
        for i in infimum..supremum + 1 {
            if cond(&i) {
                if i >= 4294967296 {
                    acc = i
                }
                if acc < 4294967296 {
                    acc *= i
                }
                if acc >= 4294967296 {
                    let carry = scale_slice(&mut cip.limbs[..], acc);

                    if carry > 0 {
                        cip.limbs.push(carry)
                    }
                    acc = 1u64;
                } // end if
            } // else
        }

        let carry = scale_slice(&mut cip.limbs[..], acc);
        if carry > 0 {
            cip.limbs.push(carry)
        }
        cip
    }
}