void PrimesTable::GenerateTableHelper(Ulong Number_, PrimesTable::TableBuffer& TempTable_) { Boolean NotPrime_; Ulong Index_, Max_; Ulong Divisor_; Max_ = Ulong(sqrt(Number_)); NotPrime_ = FALSE; for (Index_ = 1; Index_ < _TotalSize && !NotPrime_; ++Index_) { Divisor_ = GetTableElementOnGenerate(Index_, TempTable_); if (Divisor_ > Max_) break; else NotPrime_ = Number_ % Divisor_ == 0; } if (!NotPrime_) { _Bitv.SetBit(Number_); _CurTable._Table[_TableIndex++] = Number_; ++_TotalSize; } }
ChrString ULongInt::ToTruncString(const LongInt& Int_, int Radix_) { char Buffer_[16]; ULongInt UInt_(Int_); return ChrString(UlongToStr(Ulong(UInt_), Buffer_, Radix_)); }
Bool readRawText(std::string const& filename, std::string& text) { Bool is_good = true; std::ifstream in(filename.c_str(), std::ios::in | std::ios::ate | std::ios::binary); is_good = in.good(); if (is_good) { Ulong end_position = Ulong(in.tellg()); Char* script_c_str = new Char[end_position + 1UL]; in.seekg(0, std::ios::beg); in.read(script_c_str, end_position); script_c_str[end_position] = '\0'; is_good = in.good(); in.close(); text = script_c_str; delete[](script_c_str); } convertTextToNativeStyle(text); return is_good; }
Ulong PrimesTable::Floor(double Value_) { Ulong Num_, i; Num_ = Ulong(floor(Value_)); if (Num_ > MAXENTRIES) { for (i = Num_; i > MAXENTRIES; --i) if (SieveFindPrime(i)) return i; } else i = Num_; for (;i != Ulong(-1); --i) if (_Bitv[i]) return i; return 0; }
Boolean VerifyPrime(Ulong x) { Ulong max_ = Ulong(sqrt(x)); Ulong i; Ulong notprime_ = 0; for (i = 2; i <= max_ && !notprime_; ++i) if (i > max_) break; else notprime_ = x % i == 0; return !notprime_; }
Boolean PrimesTable::IsPrime(Ulong Num_, Boolean* Computable_) { Ulong Max_ = Ulong(sqrt(Num_)); if (Max_ > MAXENTRIES) { if (Computable_) *Computable_ = FALSE; } else if (Num_ > MAXENTRIES) return SieveFindPrime(Num_); else return _Bitv[Num_]; return 0; }
Ulong PrimesTable::Ceiling(double Value_) { Ulong Num_, i; Num_ = Ulong(ceil(Value_)); if (Num_ <= MAXENTRIES) { for (i = Num_; i <= MAXENTRIES; ++i) if (_Bitv[i]) return i; } else i = Num_; Ulong Max_ = 4294967295UL; for (;i < Max_; ++i) if (SieveFindPrime(i)) return i; return 0; }
Boolean PrimesTable::SieveFindPrime(Ulong Number_) { Boolean NotPrime_; Ulong Index_, Max_; Ulong Divisor_; Max_ = Ulong(sqrt(Number_)); NotPrime_ = FALSE; for (Index_ = 1; Index_ < _TotalSize && !NotPrime_; ++Index_) { Divisor_ = GetTableElement(Index_, _CurTable); if (Divisor_ > Max_) break; else NotPrime_ = Number_ % Divisor_ == 0; } return !NotPrime_; }
Ulong PrimesTable::NextPrimeFactor(Ulong Number_, Ulong& Quotient_, Ulong& Exponent_) { Boolean NotPrime_; Ulong Index_, Max_; Ulong Divisor_; Max_ = Ulong(sqrt(Number_)); NotPrime_ = FALSE; Exponent_ = 0; if (Max_ > MAXENTRIES) return 0; for (Index_ = 1; Index_ < _TotalSize && !NotPrime_; ++Index_) { Divisor_ = GetTableElement(Index_, _CurTable); if (Divisor_ > Max_) break; else { NotPrime_ = Number_ % Divisor_ == 0; if (NotPrime_) { do { Number_ /= Divisor_; ++Exponent_; } while (Number_ % Divisor_ == 0); Quotient_ = Number_; } } } return (NotPrime_ ? Divisor_:Number_); }
int main() { ChrString Str_; ULongInt si(61515012); Str_ = si.ToString(); assert(!strcmp(Str_.c_str(), "61515012")); ULongInt si2(61515012); ULongInt si3(41057012); Str_ = si3.ToString(); assert(!strcmp(Str_.c_str(), "41057012")); assert(!(si < si3)); assert(si > si3); assert(si == si2); assert(!(si == si3)); assert(si != si3); ofstream fout("testing.bin"); Write(fout, si); fout.close(); ifstream fin("testing.bin"); Read(fin, si3); fin.close(); assert(si3 == ULongInt(61515012)); si2 = "abc"; assert(si2.IsNullObject()); assert(si2.DataConversionError()); UInteger uintv("56800012"); si2.Assign(uintv); assert(si2 == ULongInt(56800012)); UShortInt usintv("65534"); si2.Assign(usintv); assert(si2 == ULongInt(65534)); cout <<"Enter a number: "; cin >>si; cout <<si <<endl; // Testing bounds checking ULongInt chk1 = ULONG_MAX; chk1 += 5; assert(chk1.Overflow()); chk1.ClearError(); chk1 = 1; --chk1; assert(!chk1.Underflow()); --chk1; assert(chk1.Underflow()); chk1.ClearError(); chk1 = 65536; chk1 *= chk1; assert(chk1.Overflow()); chk1 = 65536; chk1 >>= 2; assert(Ulong(chk1) == 16384); chk1 <<= 4; assert(Ulong(chk1) == 262144); chk1 |= ULongInt(0x0F); assert(Ulong(chk1) == 262159); chk1 = ULongInt(chk1) / ULongInt(0); assert(chk1.DivideByZero()); assert(chk1.Overflow()); assert(!chk1.NoError()); chk1.ClearError(); try { chk1.ThrowError(); } catch (const SciEngErr& ErrObj_) { cerr <<ErrObj_.message(); } // Testing series comparison class on user defined type ULongInt* List_[25]; int i; for (i = 0; i < 10; ++i) List_[i] = new ULongInt(i+1); List_[10] = NULL; ULongInt ZeroChar(0); assert(SeriesCompare<ULongInt>::SumOfUnsignedSeriesCmp(ULongInt(11), List_, ZeroChar, CHAR_MAX).Result() < 0); assert(SeriesCompare<ULongInt>::SumOfUnsignedSeriesCmp(ULongInt(55), List_, ZeroChar, CHAR_MAX).Result() == 0); assert(SeriesCompare<ULongInt>::SumOfUnsignedSeriesCmp(ULongInt(77), List_, ZeroChar, CHAR_MAX).Result() > 0); assert(SeriesCompare<ULongInt>::SumOfUnsignedSeriesCmp(ULongInt(1), List_, ZeroChar, CHAR_MAX, TRUE).Result() < 0); delete List_[5]; List_[5] = NULL; SeriesCompare<ULongInt> Pobj_ = SeriesCompare<ULongInt>::ProductOfUnsignedSeriesCmp(ULongInt(120), List_, ZeroChar, CHAR_MAX); assert(Pobj_.Result() == 0); Pobj_ = SeriesCompare<ULongInt>::ProductOfUnsignedSeriesCmp(ULongInt(121), List_, ZeroChar, CHAR_MAX); assert(Pobj_.Result() > 0); Pobj_ = SeriesCompare<ULongInt>::ProductOfUnsignedSeriesCmp(ULongInt(119), List_, ZeroChar, CHAR_MAX); assert(Pobj_.Result() < 0); for (i = 0; i < 10; ++i) delete List_[i]; cout <<endl <<"SUCCESS Testing ULongInt" <<endl; return 0; }
LongInt& ULongInt::operator = (const long& IntVal_) { _Uvalue = Ulong(IntVal_); return *this; }
ChrString ULongInt::ToTruncString(int Radix_) const { char Buffer_[16]; return ChrString(UlongToStr(Ulong(*this), Buffer_, Radix_)); }