| 1 | package Sidef::Types::Range::RangeNumber; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use parent qw( | ||
| 7 | Sidef::Types::Range::Range | ||
| 8 | ); | ||
| 9 | |||
| 10 | use overload q{""} => sub { | ||
| 11 | my ($self) = @_; | ||
| 12 | "RangeNum(" . join(', ', $self->{from}->dump, $self->{to}->dump, $self->{step}->dump) . ")"; | ||
| 13 | }; | ||
| 14 | |||
| 15 | use Sidef::Types::Bool::Bool; | ||
| 16 | use Sidef::Types::Number::Number; | ||
| 17 | use Sidef::Types::Block::Block; | ||
| 18 | |||
| 19 | # Pre-cached class names to bypass string allocations | ||
| 20 | my $NUMBER_CLASS = 'Sidef::Types::Number::Number'; | ||
| 21 | my $BLOCK_CLASS = 'Sidef::Types::Block::Block'; | ||
| 22 | |||
| 23 | # Pre-cached static block for empty/exhausted iterators | ||
| 24 | state $EMPTY_ITERATOR = CORE::bless({code => sub { undef }}, $BLOCK_CLASS); | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my (undef, $from, $to, $step) = @_; | ||
| 28 | |||
| 29 | if (not defined $from) { | ||
| 30 | $from = Sidef::Types::Number::Number::ZERO; | ||
| 31 | $to = Sidef::Types::Number::Number::MONE; | ||
| 32 | } | ||
| 33 | |||
| 34 | if (not defined $to) { | ||
| 35 | $to = $from->sub(Sidef::Types::Number::Number::ONE); | ||
| 36 | $from = Sidef::Types::Number::Number::ZERO; | ||
| 37 | } | ||
| 38 | |||
| 39 | if (ref($to) eq __PACKAGE__) { | ||
| 40 | $to = $to->{to}; | ||
| 41 | } | ||
| 42 | |||
| 43 | bless { | ||
| 44 | from => $from, | ||
| 45 | to => $to, | ||
| 46 | step => $step // Sidef::Types::Number::Number::ONE, | ||
| 47 | }, | ||
| 48 | __PACKAGE__; | ||
| 49 | } | ||
| 50 | |||
| 51 | *call = \&new; | ||
| 52 | |||
| 53 | sub iter { | ||
| 54 | my ($self) = @_; | ||
| 55 | |||
| 56 | my $step = $self->{step}; | ||
| 57 | my $from = $self->{from}; | ||
| 58 | my $to = $self->{to}; | ||
| 59 | |||
| 60 | # ------------------------------------------------------------------------- | ||
| 61 | # Fast native integers / single-word GMPz | ||
| 62 | # ------------------------------------------------------------------------- | ||
| 63 | if (ref($step) eq $NUMBER_CLASS and ref($from) eq $NUMBER_CLASS and ref($to) eq $NUMBER_CLASS) { | ||
| 64 | |||
| 65 | my $u_from = $$from; | ||
| 66 | my $u_step = $$step; | ||
| 67 | my $u_to = $$to; | ||
| 68 | |||
| 69 | my $r_from = ref($u_from); | ||
| 70 | my $r_step = ref($u_step); | ||
| 71 | my $r_to = ref($u_to); | ||
| 72 | |||
| 73 | # Extract native machine integers if available | ||
| 74 | if ( (!$r_to || ($r_to eq 'Math::GMPz' && Math::GMPz::Rmpz_fits_slong_p($u_to))) | ||
| 75 | && (!$r_from || ($r_from eq 'Math::GMPz' && Math::GMPz::Rmpz_fits_slong_p($u_from))) | ||
| 76 | && (!$r_step || ($r_step eq 'Math::GMPz' && Math::GMPz::Rmpz_fits_slong_p($u_step)))) { | ||
| 77 | |||
| 78 | my $curr = $r_from ? Math::GMPz::Rmpz_get_si($u_from) : $u_from; | ||
| 79 | my $inc = $r_step ? Math::GMPz::Rmpz_get_si($u_step) : $u_step; | ||
| 80 | my $limit = $r_to ? Math::GMPz::Rmpz_get_si($u_to) : $u_to; | ||
| 81 | |||
| 82 | #$inc || return $EMPTY_ITERATOR; | ||
| 83 | my $repetitions = ($inc == 0) ? 'Inf' : CORE::int(($limit - $curr + $inc) / $inc); | ||
| 84 | return $EMPTY_ITERATOR if $repetitions <= 0; | ||
| 85 | |||
| 86 | # Check if range guarantees no overflow of standard integer bounds | ||
| 87 | my $final_val = $curr + ($repetitions - 1) * $inc; | ||
| 88 | |||
| 89 | if ( $final_val > Sidef::Types::Number::Number::LONG_MIN | ||
| 90 | && $final_val < Sidef::Types::Number::Number::ULONG_MAX) { | ||
| 91 | |||
| 92 | # Step == 1 (Most common case) | ||
| 93 | if ($inc == 1) { | ||
| 94 | return CORE::bless( | ||
| 95 | { | ||
| 96 | code => sub { | ||
| 97 | $repetitions-- > 0 or return undef; | ||
| 98 | my $v = $curr++; | ||
| 99 | CORE::bless(\$v, $NUMBER_CLASS); | ||
| 100 | } | ||
| 101 | }, | ||
| 102 | $BLOCK_CLASS | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | |||
| 106 | # Step == -1 | ||
| 107 | if ($inc == -1) { | ||
| 108 | return CORE::bless( | ||
| 109 | { | ||
| 110 | code => sub { | ||
| 111 | $repetitions-- > 0 or return undef; | ||
| 112 | my $v = $curr--; | ||
| 113 | CORE::bless(\$v, $NUMBER_CLASS); | ||
| 114 | } | ||
| 115 | }, | ||
| 116 | $BLOCK_CLASS | ||
| 117 | ); | ||
| 118 | } | ||
| 119 | |||
| 120 | # Generic Native Integer Step | ||
| 121 | return CORE::bless( | ||
| 122 | { | ||
| 123 | code => sub { | ||
| 124 | $repetitions-- > 0 or return undef; | ||
| 125 | my $v = $curr; | ||
| 126 | $curr += $inc; | ||
| 127 | CORE::bless(\$v, $NUMBER_CLASS); | ||
| 128 | } | ||
| 129 | }, | ||
| 130 | $BLOCK_CLASS | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | # ------------------------------------------------------------------------- | ||
| 136 | # Pure Math::GMPz range iteration (Heavy/Overflow Path) | ||
| 137 | # ------------------------------------------------------------------------- | ||
| 138 | if ( (!$r_to || ($r_to eq 'Math::GMPz')) | ||
| 139 | && (!$r_from || ($r_from eq 'Math::GMPz')) | ||
| 140 | && (!$r_step || ($r_step eq 'Math::GMPz'))) { | ||
| 141 | |||
| 142 | my $mpz_from = $r_from ? $u_from : ($u_from > 0 ? Math::GMPz::Rmpz_init_set_ui($u_from) : Math::GMPz::Rmpz_init_set_si($u_from)); | ||
| 143 | my $curr_mpz = Math::GMPz::Rmpz_init_set($mpz_from); | ||
| 144 | my $times_mpz = $self->{_times}; | ||
| 145 | |||
| 146 | # Calculate repetition count via GMPz | ||
| 147 | if (!defined $times_mpz) { | ||
| 148 | my $mpz_step = $r_step ? $u_step : ($u_step > 0 ? Math::GMPz::Rmpz_init_set_ui($u_step) : Math::GMPz::Rmpz_init_set_si($u_step)); | ||
| 149 | my $mpz_to = $r_to ? $u_to : ($u_to > 0 ? Math::GMPz::Rmpz_init_set_ui($u_to) : Math::GMPz::Rmpz_init_set_si($u_to)); | ||
| 150 | |||
| 151 | $times_mpz = Math::GMPz::Rmpz_init(); | ||
| 152 | Math::GMPz::Rmpz_sub($times_mpz, $mpz_to, $mpz_from); | ||
| 153 | Math::GMPz::Rmpz_add($times_mpz, $times_mpz, $mpz_step); | ||
| 154 | |||
| 155 | if (Math::GMPz::Rmpz_cmp_ui($mpz_step, 1) != 0 and Math::GMPz::Rmpz_sgn($mpz_step) != 0) { | ||
| 156 | Math::GMPz::Rmpz_tdiv_q($times_mpz, $times_mpz, $mpz_step); | ||
| 157 | } | ||
| 158 | |||
| 159 | $self->{_times} = $times_mpz; | ||
| 160 | } | ||
| 161 | |||
| 162 | return $EMPTY_ITERATOR if Math::GMPz::Rmpz_sgn($times_mpz) <= 0; | ||
| 163 | |||
| 164 | # Repetitions stored as native counter if it fits | ||
| 165 | if (Math::GMPz::Rmpz_fits_ulong_p($times_mpz)) { | ||
| 166 | my $rep_count = Math::GMPz::Rmpz_get_ui($times_mpz); | ||
| 167 | |||
| 168 | # GMPz with native step | ||
| 169 | if (!$r_step and $u_step >= 0 and $u_step < Sidef::Types::Number::Number::ULONG_MAX) { | ||
| 170 | return CORE::bless( | ||
| 171 | { | ||
| 172 | code => sub { | ||
| 173 | $rep_count-- > 0 or return undef; | ||
| 174 | my $out_mpz = Math::GMPz::Rmpz_init_set($curr_mpz); | ||
| 175 | Math::GMPz::Rmpz_add_ui($curr_mpz, $curr_mpz, $u_step); | ||
| 176 | CORE::bless(\$out_mpz, $NUMBER_CLASS); | ||
| 177 | } | ||
| 178 | }, | ||
| 179 | $BLOCK_CLASS | ||
| 180 | ); | ||
| 181 | } | ||
| 182 | |||
| 183 | # Generic GMPz Step | ||
| 184 | my $mpz_step = $r_step ? $u_step : ($u_step > 0 ? Math::GMPz::Rmpz_init_set_ui($u_step) : Math::GMPz::Rmpz_init_set_si($u_step)); | ||
| 185 | return CORE::bless( | ||
| 186 | { | ||
| 187 | code => sub { | ||
| 188 | $rep_count-- > 0 or return undef; | ||
| 189 | my $out_mpz = Math::GMPz::Rmpz_init_set($curr_mpz); | ||
| 190 | Math::GMPz::Rmpz_add($curr_mpz, $curr_mpz, $mpz_step); | ||
| 191 | CORE::bless(\$out_mpz, $NUMBER_CLASS); | ||
| 192 | } | ||
| 193 | }, | ||
| 194 | $BLOCK_CLASS | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | # ------------------------------------------------------------------------- | ||
| 200 | # Pure Math::GMPz range iteration: infinite positive (ascending) range | ||
| 201 | # ------------------------------------------------------------------------- | ||
| 202 | if ( ($r_to eq 'Math::MPFR' and Math::MPFR::Rmpfr_inf_p($u_to) and Math::MPFR::Rmpfr_sgn($u_to) > 0) | ||
| 203 | && (!$r_from || ($r_from eq 'Math::GMPz')) | ||
| 204 | && (!$r_step || ($r_step eq 'Math::GMPz'))) { | ||
| 205 | |||
| 206 | my $mpz_from = $r_from ? $u_from : ($u_from > 0 ? Math::GMPz::Rmpz_init_set_ui($u_from) : Math::GMPz::Rmpz_init_set_si($u_from)); | ||
| 207 | my $curr_mpz = Math::GMPz::Rmpz_init_set($mpz_from); | ||
| 208 | |||
| 209 | # GMPz with native step | ||
| 210 | if (!$r_step and $u_step > 0 and $u_step < Sidef::Types::Number::Number::ULONG_MAX) { | ||
| 211 | return CORE::bless( | ||
| 212 | { | ||
| 213 | code => sub { | ||
| 214 | my $out_mpz = Math::GMPz::Rmpz_init_set($curr_mpz); | ||
| 215 | Math::GMPz::Rmpz_add_ui($curr_mpz, $curr_mpz, $u_step); | ||
| 216 | CORE::bless(\$out_mpz, $NUMBER_CLASS); | ||
| 217 | } | ||
| 218 | }, | ||
| 219 | $BLOCK_CLASS | ||
| 220 | ); | ||
| 221 | } | ||
| 222 | |||
| 223 | if (!$r_step and $u_step < 0) { | ||
| 224 | return $EMPTY_ITERATOR; | ||
| 225 | } | ||
| 226 | |||
| 227 | # Generic GMPz Step | ||
| 228 | my $mpz_step = $r_step ? $u_step : ($u_step > 0 ? Math::GMPz::Rmpz_init_set_ui($u_step) : Math::GMPz::Rmpz_init_set_si($u_step)); | ||
| 229 | Math::GMPz::Rmpz_sgn($mpz_step) >= 0 or return $EMPTY_ITERATOR; | ||
| 230 | |||
| 231 | return CORE::bless( | ||
| 232 | { | ||
| 233 | code => sub { | ||
| 234 | my $out_mpz = Math::GMPz::Rmpz_init_set($curr_mpz); | ||
| 235 | Math::GMPz::Rmpz_add($curr_mpz, $curr_mpz, $mpz_step); | ||
| 236 | CORE::bless(\$out_mpz, $NUMBER_CLASS); | ||
| 237 | } | ||
| 238 | }, | ||
| 239 | $BLOCK_CLASS | ||
| 240 | ); | ||
| 241 | |||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | # ------------------------------------------------------------------------- | ||
| 246 | # Generic Fallback Path (Objects, Fractions, Reals, Floats) | ||
| 247 | # ------------------------------------------------------------------------- | ||
| 248 | my $asc = ($self->{_asc} //= !!($step->is_pos // return $EMPTY_ITERATOR)); | ||
| 249 | |||
| 250 | return CORE::bless( | ||
| 251 | { | ||
| 252 | code => $asc | ||
| 253 | ? sub { | ||
| 254 | $from->le($to) || return undef; | ||
| 255 | my $tmp = $from; | ||
| 256 | $from = $from->add($step); | ||
| 257 | return $tmp; | ||
| 258 | } | ||
| 259 | : sub { | ||
| 260 | $from->ge($to) || return undef; | ||
| 261 | my $tmp = $from; | ||
| 262 | $from = $from->add($step); | ||
| 263 | return $tmp; | ||
| 264 | } | ||
| 265 | }, | ||
| 266 | $BLOCK_CLASS | ||
| 267 | ); | ||
| 268 | } | ||
| 269 | |||
| 270 | sub _reduce_by { | ||
| 271 | my ($self, $method, $result, $callback) = @_; | ||
| 272 | |||
| 273 | my @list; | ||
| 274 | my $count = 0; | ||
| 275 | |||
| 276 | my $iter = $self->iter; | ||
| 277 | |||
| 278 | while (1) { | ||
| 279 | push @list, $callback->($iter->run() // last); | ||
| 280 | |||
| 281 | if (++$count > 1e5) { | ||
| 282 | $count = 0; | ||
| 283 | $result = $result->$method(splice(@list)); | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | if (@list) { | ||
| 288 | $result = $result->$method(splice(@list)); | ||
| 289 | } | ||
| 290 | |||
| 291 | $result; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub sum_by { | ||
| 295 | my ($self, $block) = @_; | ||
| 296 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 297 | $self->_reduce_by('sum', Sidef::Types::Number::Number::ZERO, sub { $block->run($_[0]) }); | ||
| 298 | } | ||
| 299 | |||
| 300 | sub sum { | ||
| 301 | my ($self, $arg) = @_; | ||
| 302 | |||
| 303 | if (defined($arg)) { | ||
| 304 | goto &sum_by; | ||
| 305 | } | ||
| 306 | |||
| 307 | # Formula: | ||
| 308 | # sum(x .. y `by` z) = (floor((y - x)/z) + 1) * (z * floor((y - x)/z) + 2*x) / 2 | ||
| 309 | |||
| 310 | my ($x, $y, $z) = @{$self}{'from', 'to', 'step'}; | ||
| 311 | |||
| 312 | my $n = $y->sub($x)->div($z); | ||
| 313 | |||
| 314 | if ($n->is_neg) { | ||
| 315 | return Sidef::Types::Number::Number::ZERO; | ||
| 316 | } | ||
| 317 | |||
| 318 | state $two = Sidef::Types::Number::Number::_set_int(2); | ||
| 319 | |||
| 320 | $n = $n->floor; | ||
| 321 | $n->inc->mul($z->mul($n)->add($x->mul($two)))->div($two); | ||
| 322 | } | ||
| 323 | |||
| 324 | *Σ = \∑ | ||
| 325 | |||
| 326 | sub avg_by { | ||
| 327 | my ($self, $block) = @_; | ||
| 328 | $self->sum_by($block)->div($self->len); | ||
| 329 | } | ||
| 330 | |||
| 331 | sub avg { | ||
| 332 | my ($self, $arg) = @_; | ||
| 333 | |||
| 334 | if (defined($arg)) { | ||
| 335 | goto &avg_by; | ||
| 336 | } | ||
| 337 | |||
| 338 | $self->sum->div($self->len); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub prod_by { | ||
| 342 | my ($self, $block) = @_; | ||
| 343 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 344 | $self->_reduce_by('prod', Sidef::Types::Number::Number::ONE, sub { $block->run($_[0]) }); | ||
| 345 | } | ||
| 346 | |||
| 347 | sub prod { | ||
| 348 | my ($self, $arg) = @_; | ||
| 349 | |||
| 350 | if (defined($arg)) { | ||
| 351 | goto &prod_by; | ||
| 352 | } | ||
| 353 | |||
| 354 | if ( $self->{step}->is_one | ||
| 355 | and $self->{from}->is_one | ||
| 356 | and $self->{to}->is_pos) { | ||
| 357 | return $self->{to}->factorial; | ||
| 358 | } | ||
| 359 | |||
| 360 | Sidef::Types::Number::Number::prod($self->to_list); | ||
| 361 | } | ||
| 362 | |||
| 363 | *Î = \∏ | ||
| 364 | |||
| 365 | sub lcm_by { | ||
| 366 | my ($self, $block) = @_; | ||
| 367 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 368 | $self->_reduce_by('lcm', Sidef::Types::Number::Number::ONE, sub { $block->run($_[0]) }); | ||
| 369 | } | ||
| 370 | |||
| 371 | sub lcm { | ||
| 372 | my ($self, $arg) = @_; | ||
| 373 | |||
| 374 | if (defined($arg)) { | ||
| 375 | goto &lcm_by; | ||
| 376 | } | ||
| 377 | |||
| 378 | if ( $self->{step}->is_one | ||
| 379 | and $self->{from}->is_one | ||
| 380 | and $self->{to}->is_pos) { | ||
| 381 | return $self->{to}->consecutive_lcm; | ||
| 382 | } | ||
| 383 | |||
| 384 | Sidef::Types::Number::Number::lcm($self->to_list); | ||
| 385 | } | ||
| 386 | |||
| 387 | sub gcd_by { | ||
| 388 | my ($self, $block) = @_; | ||
| 389 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 390 | $self->_reduce_by('gcd', Sidef::Types::Number::Number::ZERO, sub { $block->run($_[0]) }); | ||
| 391 | } | ||
| 392 | |||
| 393 | sub gcd { | ||
| 394 | my ($self, $arg) = @_; | ||
| 395 | |||
| 396 | if (defined($arg)) { | ||
| 397 | goto &gcd_by; | ||
| 398 | } | ||
| 399 | |||
| 400 | Sidef::Types::Number::Number::gcd($self->to_list); | ||
| 401 | } | ||
| 402 | |||
| 403 | sub bsearch { | ||
| 404 | my ($self, $block) = @_; | ||
| 405 | |||
| 406 | if ($self->{step}->is_one) { | ||
| 407 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 408 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 409 | return Sidef::Types::Number::Number::bsearch($left, $right, $block); | ||
| 410 | } | ||
| 411 | |||
| 412 | $self->to_a->bsearch($block); | ||
| 413 | } | ||
| 414 | |||
| 415 | sub bsearch_le { | ||
| 416 | my ($self, $block) = @_; | ||
| 417 | |||
| 418 | if ($self->{step}->is_one) { | ||
| 419 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 420 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 421 | return Sidef::Types::Number::Number::bsearch_le($left, $right, $block); | ||
| 422 | } | ||
| 423 | |||
| 424 | $self->to_a->bsearch_le($block); | ||
| 425 | } | ||
| 426 | |||
| 427 | sub bsearch_ge { | ||
| 428 | my ($self, $block) = @_; | ||
| 429 | |||
| 430 | if ($self->{step}->is_one) { | ||
| 431 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 432 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 433 | return Sidef::Types::Number::Number::bsearch_ge($left, $right, $block); | ||
| 434 | } | ||
| 435 | |||
| 436 | $self->to_a->bsearch_ge($block); | ||
| 437 | } | ||
| 438 | |||
| 439 | sub faulhaber_sum { | ||
| 440 | my ($self, $k) = @_; | ||
| 441 | |||
| 442 | $k = Sidef::Types::Number::Number->new($k); | ||
| 443 | |||
| 444 | if ($self->{step}->is_one) { | ||
| 445 | |||
| 446 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 447 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 448 | |||
| 449 | return Sidef::Types::Number::Number::faulhaber_range($left, $right, $k); | ||
| 450 | } | ||
| 451 | |||
| 452 | $self->lazy->map(Sidef::Types::Block::Block->new(code => sub { $_[0]->ipow($k) }))->sum; | ||
| 453 | } | ||
| 454 | |||
| 455 | *faulhaber = \&faulhaber_sum; | ||
| 456 | |||
| 457 | sub mertens { | ||
| 458 | my ($self) = @_; | ||
| 459 | |||
| 460 | if ($self->{step}->is_one) { | ||
| 461 | |||
| 462 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 463 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 464 | |||
| 465 | return Sidef::Types::Number::Number::mertens($left, $right); | ||
| 466 | } | ||
| 467 | |||
| 468 | $self->lazy->grep(Sidef::Types::Block::Block->new(code => sub { $_[0]->is_squarefree })) | ||
| 469 | ->map(Sidef::Types::Block::Block->new(code => sub { $_[0]->moebius })) | ||
| 470 | ->sum; | ||
| 471 | } | ||
| 472 | |||
| 473 | sub dump { | ||
| 474 | my ($self) = @_; | ||
| 475 | Sidef::Types::String::String->new("$self"); | ||
| 476 | } | ||
| 477 | |||
| 478 | *to_s = \&dump; | ||
| 479 | *to_str = \&dump; | ||
| 480 | |||
| 481 | { | ||
| 482 | no strict 'refs'; | ||
| 483 | |||
| 484 | { | ||
| 485 | my @methods = ( | ||
| 486 | { | ||
| 487 | each_name => 'each_squarefree', | ||
| 488 | arr_name => 'squarefree', | ||
| 489 | count_name => 'squarefree_count', | ||
| 490 | sum_name => 'squarefree_sum', | ||
| 491 | predicate => sub { $_[0]->is_squarefree }, | ||
| 492 | }, | ||
| 493 | { | ||
| 494 | each_name => 'each_cubefree', | ||
| 495 | arr_name => 'cubefree', | ||
| 496 | count_name => 'cubefree_count', | ||
| 497 | sum_name => 'cubefree_sum', | ||
| 498 | predicate => sub { $_[0]->is_cubefree }, | ||
| 499 | }, | ||
| 500 | { | ||
| 501 | each_name => 'each_squarefull', | ||
| 502 | arr_name => 'squarefull', | ||
| 503 | count_name => 'squarefull_count', | ||
| 504 | sum_name => 'squarefull_sum', | ||
| 505 | predicate => sub { $_[0]->is_squarefull }, | ||
| 506 | }, | ||
| 507 | { | ||
| 508 | each_name => 'each_cubefull', | ||
| 509 | arr_name => 'cubefull', | ||
| 510 | count_name => 'cubefull_count', | ||
| 511 | sum_name => 'cubefull_sum', | ||
| 512 | predicate => sub { $_[0]->is_cubefull }, | ||
| 513 | }, | ||
| 514 | { | ||
| 515 | each_name => 'each_nonsquarefree', | ||
| 516 | arr_name => 'nonsquarefree', | ||
| 517 | count_name => 'nonsquarefree_count', | ||
| 518 | sum_name => 'nonsquarefree_sum', | ||
| 519 | predicate => sub { $_[0]->is_nonsquarefree }, | ||
| 520 | }, | ||
| 521 | { | ||
| 522 | each_name => 'each_noncubefree', | ||
| 523 | arr_name => 'noncubefree', | ||
| 524 | count_name => 'noncubefree_count', | ||
| 525 | sum_name => 'noncubefree_sum', | ||
| 526 | predicate => sub { $_[0]->is_noncubefree }, | ||
| 527 | }, | ||
| 528 | { | ||
| 529 | each_name => 'each_semiprime', | ||
| 530 | arr_name => 'semiprimes', | ||
| 531 | count_name => 'semiprime_count', | ||
| 532 | sum_name => 'semiprime_sum', | ||
| 533 | predicate => sub { $_[0]->is_semiprime }, | ||
| 534 | }, | ||
| 535 | { | ||
| 536 | each_name => 'each_squarefree_semiprime', | ||
| 537 | arr_name => 'squarefree_semiprimes', | ||
| 538 | count_name => 'squarefree_semiprime_count', | ||
| 539 | sum_name => 'squarefree_semiprime_sum', | ||
| 540 | predicate => sub { $_[0]->is_squarefree_semiprime }, | ||
| 541 | }, | ||
| 542 | { | ||
| 543 | each_name => 'each_composite', | ||
| 544 | arr_name => 'composites', | ||
| 545 | count_name => 'composite_count', | ||
| 546 | sum_name => 'composite_sum', | ||
| 547 | predicate => sub { $_[0]->is_composite }, | ||
| 548 | }, | ||
| 549 | { | ||
| 550 | each_name => 'each_prime', | ||
| 551 | arr_name => 'primes', | ||
| 552 | count_name => 'prime_count', | ||
| 553 | sum_name => 'prime_sum', | ||
| 554 | predicate => sub { $_[0]->is_prime }, | ||
| 555 | }, | ||
| 556 | { | ||
| 557 | each_name => 'each_prime_power', | ||
| 558 | arr_name => 'prime_powers', | ||
| 559 | count_name => 'prime_power_count', | ||
| 560 | sum_name => 'prime_power_sum', | ||
| 561 | predicate => sub { $_[0]->is_prime_power }, | ||
| 562 | }, | ||
| 563 | ); | ||
| 564 | |||
| 565 | foreach my $method (@methods) { | ||
| 566 | |||
| 567 | # Each | ||
| 568 | if (defined($method->{each_name})) { | ||
| 569 | *{__PACKAGE__ . '::' . $method->{each_name}} = sub { | ||
| 570 | my ($self, $block) = @_; | ||
| 571 | |||
| 572 | if ($self->{step}->is_one) { | ||
| 573 | |||
| 574 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 575 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 576 | |||
| 577 | &{'Sidef::Types::Number::Number::' . $method->{each_name}}($left, $right, $block); | ||
| 578 | return $self; | ||
| 579 | } | ||
| 580 | |||
| 581 | $self->lazy->grep(Sidef::Types::Block::Block->new(code => $method->{predicate}))->each($block); | ||
| 582 | return $self; | ||
| 583 | }; | ||
| 584 | } | ||
| 585 | |||
| 586 | # Array | ||
| 587 | if (defined($method->{arr_name})) { | ||
| 588 | *{__PACKAGE__ . '::' . $method->{arr_name}} = sub { | ||
| 589 | my ($self) = @_; | ||
| 590 | |||
| 591 | if ($self->{step}->abs->is_one) { | ||
| 592 | |||
| 593 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 594 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 595 | |||
| 596 | if ($self->{step}->is_neg) { | ||
| 597 | return &{'Sidef::Types::Number::Number::' . $method->{arr_name}}($right, $left)->flip; | ||
| 598 | } | ||
| 599 | |||
| 600 | return &{'Sidef::Types::Number::Number::' . $method->{arr_name}}($left, $right); | ||
| 601 | } | ||
| 602 | |||
| 603 | $self->lazy->grep(Sidef::Types::Block::Block->new(code => $method->{predicate})); | ||
| 604 | }; | ||
| 605 | } | ||
| 606 | |||
| 607 | # Sum | ||
| 608 | if (defined($method->{sum_name})) { | ||
| 609 | *{__PACKAGE__ . '::' . $method->{sum_name}} = sub { | ||
| 610 | my ($self) = @_; | ||
| 611 | |||
| 612 | if ($self->{step}->abs->is_one) { | ||
| 613 | |||
| 614 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 615 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 616 | |||
| 617 | if ($self->{step}->is_neg) { | ||
| 618 | ($right, $left) = ($left, $right); | ||
| 619 | } | ||
| 620 | |||
| 621 | return &{'Sidef::Types::Number::Number::' . $method->{sum_name}}($left, $right); | ||
| 622 | } | ||
| 623 | |||
| 624 | $self->lazy->grep(Sidef::Types::Block::Block->new(code => $method->{predicate}))->sum; | ||
| 625 | }; | ||
| 626 | } | ||
| 627 | |||
| 628 | # Count | ||
| 629 | if (defined($method->{count_name})) { | ||
| 630 | *{__PACKAGE__ . '::' . $method->{count_name}} = sub { | ||
| 631 | my ($self) = @_; | ||
| 632 | |||
| 633 | if ($self->{step}->abs->is_one) { | ||
| 634 | |||
| 635 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 636 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 637 | |||
| 638 | if ($self->{step}->is_neg) { | ||
| 639 | ($right, $left) = ($left, $right); | ||
| 640 | } | ||
| 641 | |||
| 642 | return &{'Sidef::Types::Number::Number::' . $method->{count_name}}($left, $right); | ||
| 643 | } | ||
| 644 | |||
| 645 | $self->count_by(Sidef::Types::Block::Block->new(code => $method->{predicate})); | ||
| 646 | }; | ||
| 647 | } | ||
| 648 | } | ||
| 649 | } | ||
| 650 | |||
| 651 | { | ||
| 652 | my @methods = ( | ||
| 653 | { | ||
| 654 | arr_name => 'squarefree_almost_primes', | ||
| 655 | each_name => 'each_squarefree_almost_prime', | ||
| 656 | count_name => 'squarefree_almost_prime_count', | ||
| 657 | sum_name => 'squarefree_almost_prime_sum', | ||
| 658 | predicate => sub { | ||
| 659 | my ($k) = @_; | ||
| 660 | sub { $_[0]->is_squarefree_almost_prime($k) }; | ||
| 661 | }, | ||
| 662 | }, | ||
| 663 | { | ||
| 664 | arr_name => 'carmichael', | ||
| 665 | each_name => 'each_carmichael', | ||
| 666 | predicate => sub { | ||
| 667 | my ($k) = @_; | ||
| 668 | sub { $_[0]->is_carmichael && $_[0]->is_almost_prime($k) }; | ||
| 669 | }, | ||
| 670 | }, | ||
| 671 | { | ||
| 672 | arr_name => 'lucas_carmichael', | ||
| 673 | each_name => 'each_lucas_carmichael', | ||
| 674 | predicate => sub { | ||
| 675 | my ($k) = @_; | ||
| 676 | sub { $_[0]->is_lucas_carmichael && $_[0]->is_almost_prime($k) }; | ||
| 677 | }, | ||
| 678 | }, | ||
| 679 | { | ||
| 680 | arr_name => 'omega_primes', | ||
| 681 | each_name => 'each_omega_prime', | ||
| 682 | count_name => 'omega_prime_count', | ||
| 683 | sum_name => 'omega_prime_sum', | ||
| 684 | predicate => sub { | ||
| 685 | my ($k) = @_; | ||
| 686 | sub { $_[0]->is_omega_prime($k) }; | ||
| 687 | }, | ||
| 688 | }, | ||
| 689 | { | ||
| 690 | arr_name => 'almost_primes', | ||
| 691 | each_name => 'each_almost_prime', | ||
| 692 | count_name => 'almost_prime_count', | ||
| 693 | sum_name => 'almost_prime_sum', | ||
| 694 | predicate => sub { | ||
| 695 | my ($k) = @_; | ||
| 696 | sub { $_[0]->is_almost_prime($k) }; | ||
| 697 | }, | ||
| 698 | }, | ||
| 699 | { | ||
| 700 | arr_name => 'powerful', | ||
| 701 | each_name => 'each_powerful', | ||
| 702 | count_name => 'powerful_count', | ||
| 703 | sum_name => 'powerful_sum', | ||
| 704 | predicate => sub { | ||
| 705 | my ($k) = @_; | ||
| 706 | sub { $_[0]->is_powerful($k) }; | ||
| 707 | }, | ||
| 708 | }, | ||
| 709 | { | ||
| 710 | arr_name => 'nonpowerfree', | ||
| 711 | each_name => 'each_nonpowerfree', | ||
| 712 | count_name => 'nonpowerfree_count', | ||
| 713 | sum_name => 'nonpowerfree_sum', | ||
| 714 | predicate => sub { | ||
| 715 | my ($k) = @_; | ||
| 716 | sub { $_[0]->is_nonpowerfree($k) }; | ||
| 717 | }, | ||
| 718 | }, | ||
| 719 | { | ||
| 720 | arr_name => 'powerfree', | ||
| 721 | each_name => 'each_powerfree', | ||
| 722 | count_name => 'powerfree_count', | ||
| 723 | sum_name => 'powerfree_sum', | ||
| 724 | predicate => sub { | ||
| 725 | my ($k) = @_; | ||
| 726 | sub { $_[0]->is_powerfree($k) }; | ||
| 727 | }, | ||
| 728 | }, | ||
| 729 | { | ||
| 730 | count_name => 'smooth_count', | ||
| 731 | predicate => sub { | ||
| 732 | my ($k) = @_; | ||
| 733 | sub { $_[0]->is_smooth($k) }; | ||
| 734 | }, | ||
| 735 | }, | ||
| 736 | { | ||
| 737 | count_name => 'rough_count', | ||
| 738 | predicate => sub { | ||
| 739 | my ($k) = @_; | ||
| 740 | sub { $_[0]->is_rough($k) }; | ||
| 741 | }, | ||
| 742 | }, | ||
| 743 | ); | ||
| 744 | |||
| 745 | foreach my $method (@methods) { | ||
| 746 | |||
| 747 | # Each | ||
| 748 | if (defined($method->{each_name})) { | ||
| 749 | *{__PACKAGE__ . '::' . $method->{each_name}} = sub { | ||
| 750 | my ($self, $k, $block) = @_; | ||
| 751 | |||
| 752 | $k = Sidef::Types::Number::Number->new($k); | ||
| 753 | |||
| 754 | if ($self->{step}->is_one) { | ||
| 755 | |||
| 756 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 757 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 758 | |||
| 759 | &{'Sidef::Types::Number::Number::' . $method->{each_name}}($k, $left, $right, $block); | ||
| 760 | return $self; | ||
| 761 | } | ||
| 762 | |||
| 763 | $self->lazy->grep(Sidef::Types::Block::Block->new(code => $method->{predicate}->($k)))->each($block); | ||
| 764 | return $self; | ||
| 765 | }; | ||
| 766 | } | ||
| 767 | |||
| 768 | # Array | ||
| 769 | if (defined($method->{arr_name})) { | ||
| 770 | *{__PACKAGE__ . '::' . $method->{arr_name}} = sub { | ||
| 771 | my ($self, $k) = @_; | ||
| 772 | |||
| 773 | $k = Sidef::Types::Number::Number->new($k); | ||
| 774 | |||
| 775 | if ($self->{step}->abs->is_one) { | ||
| 776 | |||
| 777 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 778 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 779 | |||
| 780 | if ($self->{step}->is_neg) { | ||
| 781 | return &{'Sidef::Types::Number::Number::' . $method->{arr_name}}($k, $right, $left)->flip; | ||
| 782 | } | ||
| 783 | |||
| 784 | return &{'Sidef::Types::Number::Number::' . $method->{arr_name}}($k, $left, $right); | ||
| 785 | } | ||
| 786 | |||
| 787 | $self->lazy->grep(Sidef::Types::Block::Block->new(code => $method->{predicate}->($k))); | ||
| 788 | }; | ||
| 789 | } | ||
| 790 | |||
| 791 | # Sum | ||
| 792 | if (defined($method->{sum_name})) { | ||
| 793 | *{__PACKAGE__ . '::' . $method->{sum_name}} = sub { | ||
| 794 | my ($self, $k) = @_; | ||
| 795 | |||
| 796 | $k = Sidef::Types::Number::Number->new($k); | ||
| 797 | |||
| 798 | if ($self->{step}->abs->is_one) { | ||
| 799 | |||
| 800 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 801 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 802 | |||
| 803 | if ($self->{step}->is_neg) { | ||
| 804 | ($right, $left) = ($left, $right); | ||
| 805 | } | ||
| 806 | |||
| 807 | return &{'Sidef::Types::Number::Number::' . $method->{sum_name}}($k, $left, $right); | ||
| 808 | } | ||
| 809 | |||
| 810 | $self->lazy->grep(Sidef::Types::Block::Block->new(code => $method->{predicate}->($k)))->sum; | ||
| 811 | }; | ||
| 812 | } | ||
| 813 | |||
| 814 | # Count | ||
| 815 | if (defined($method->{count_name})) { | ||
| 816 | |||
| 817 | *{__PACKAGE__ . '::' . $method->{count_name}} = sub { | ||
| 818 | my ($self, $k) = @_; | ||
| 819 | |||
| 820 | $k = Sidef::Types::Number::Number->new($k); | ||
| 821 | |||
| 822 | if ($self->{step}->abs->is_one) { | ||
| 823 | |||
| 824 | my $left = Sidef::Types::Number::Number->new($self->{from}); | ||
| 825 | my $right = Sidef::Types::Number::Number->new($self->{to}); | ||
| 826 | |||
| 827 | if ($self->{step}->is_neg) { | ||
| 828 | ($right, $left) = ($left, $right); | ||
| 829 | } | ||
| 830 | |||
| 831 | return &{'Sidef::Types::Number::Number::' . $method->{count_name}}($k, $left, $right); | ||
| 832 | } | ||
| 833 | |||
| 834 | $self->count_by(Sidef::Types::Block::Block->new(code => $method->{predicate}->($k))); | ||
| 835 | }; | ||
| 836 | |||
| 837 | } | ||
| 838 | } | ||
| 839 | } | ||
| 840 | } | ||
| 841 | |||
| 842 | 1 |