-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the Number.modinv() (and Number.invmod() as alias)
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
Showing
6 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,7 @@ package Sidef::Optimizer { | |
round roundf | ||
digit | ||
nok | ||
modinv | ||
rdiv | ||
is_div | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |