Skip to content

Commit

Permalink
- Added the Number.modinv() (and Number.invmod() as alias)
Browse files Browse the repository at this point in the history
new file:   scripts/RosettaCode/function_frequency.sf
new file:   scripts/RosettaCode/modular_inverse.sf
  • Loading branch information
trizen committed Dec 13, 2015
1 parent 160c97b commit b049eb0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ scripts/RosettaCode/formatted_numeric_output.sf
scripts/RosettaCode/formatted_numeric_output_1.sf
scripts/RosettaCode/function_composition.sf
scripts/RosettaCode/function_definition.sf
scripts/RosettaCode/function_frequency.sf
scripts/RosettaCode/gamma_function.sf
scripts/RosettaCode/gamma_function_1.sf
scripts/RosettaCode/general_fizzbuzz.sf
Expand Down Expand Up @@ -400,6 +401,7 @@ scripts/RosettaCode/maze_generation.sf
scripts/RosettaCode/md5.sf
scripts/RosettaCode/md5_1.sf
scripts/RosettaCode/middle_three_digits.sf
scripts/RosettaCode/modular_inverse.sf
scripts/RosettaCode/move_to_front_algorithm_1.sf
scripts/RosettaCode/move_to_front_algorithm_2.sf
scripts/RosettaCode/multifactorial.sf
Expand Down
1 change: 1 addition & 0 deletions lib/Sidef/Optimizer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ package Sidef::Optimizer {
round roundf
digit
nok
modinv
rdiv
is_div
Expand Down
7 changes: 7 additions & 0 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ package Sidef::Types::Number::Number {

*expmod = \&modpow;

sub modinv {
my ($self, $mod) = @_;
$self->new($$self->as_int->bmodinv($mod->get_value->as_int));
}

*invmod = \&modinv;

sub pow {
my ($self, $num) = @_;
$self->new($$self->copy->bpow($num->get_value));
Expand Down
10 changes: 10 additions & 0 deletions lib/Sidef/Types/Number/Number.pod
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,16 @@ Aliases: I<to_i>, I<as_int>, I<to_int>

=cut

=head2 invmod

Number.invmod() -> I<Obj>

Return the

Aliases: I<modinv>

=cut

=head2 is_even

Number.is_even() -> I<Obj>
Expand Down
25 changes: 25 additions & 0 deletions scripts/RosettaCode/function_frequency.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Function_frequency
#

var program = <<'EOT'
func foo { }
func bar { }
foo(); foo(); foo()
bar(); bar();
EOT

var parser = Parser.new # initialize a new parser
parser.parse_script( code => program ) # parse the code

var data = Perl.to_sidef(parser{:vars}{:main}).flatten

data.sort_by { |v| -v{:count} }.first(10).each { |entry|
if (entry{:type} == :func) {
say ("Function `#{entry{:name}}` (declared at line",
" #{entry{:line}}) is used #{entry{:count}} times")
}
}
18 changes: 18 additions & 0 deletions scripts/RosettaCode/modular_inverse.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/ruby

func invmod(a, n) {
var (t, nt, r, nr) = (0, 1, n, a % n)
while (nr != 0) {
var quot = int((r - (r % nr)) / nr);
(nt, t) = (t - quot*nt, nt);
(nr, r) = (r - quot*nr, nr);
}
r > 1 && return()
t < 0 && (t += n)
t
}

var n = invmod(42, 2017)

say n
assert_eq(42.modinv(2017), n)

0 comments on commit b049eb0

Please sign in to comment.