TAILIEUCHUNG - Mastering Algorithms with Perl phần 8

Điều trị Remainders Trung Quốc là số nguyên Cũng như bạn có thể thêm, trừ, và nhân các số nguyên thường xuyên, bạn có thể làm tương tự với Trung Quốc còn lại. Những đại diện này có thể được thao tác bằng cách sử dụng các thói quen như add_chinese (), có thêm hai đại diện Trung Quốc: phá vỡ | all clear add it to the product prod _ @inverses map my k prod _ k mod_inverse k _ @_ - - Convert from a list of remainders into an integer. sub from_chinese use integer my v shift my t 0 for 0. bases t inverses _ v- _ return t prod Convert from an integer into a list of remainders. sub to_chinese use integer my v shift my @v map v _ @bases return @v How many eggs did the woman have set_chinese 3 4 5 7 print from_chinese 1 1 1 0 prints 301 Treating Chinese Remainders As Integers Just as you can add subtract and multiply regular integers you can do the same with Chinese remainders. These representations could be manipulated using routines such as add_chinese which adds two Chinese representations break Add two Chinese remainder lists. sub add_chinese use integer my v1 v2 @_ my @v map v1- _ v2- _ bases _ 0 . bases return @v Page 517 By changing the to a or - you can define multiply_chinese and subtract_chinese similarly. Since adding subtracting and multiplying remainders yields the same result as adding subtracting or multiplying the corresponding integers we can perform arithmetic with whichever representation we like. Integer Exponentiation Perl has a built-in exponentiation operator but it s useful to understand how exponentiation works for integers so that we can modify it for modular arithmetic. Here s a simple-minded subroutine that computes i j sub exp_slow use integer my result i j 1 @_ result i while j-- return result That s fine when the numbers are small but what about when you are dealing with hundred digit numbers That while loop will run until the universe collapses. Fortunately we don t have to perform the multiplications one by one thanks to the following identity x y x z x y z Here is one variation that uses that identity. It uses the fact that if j is even it can be written as 2k and i2k ik ik. If j is odd it can be written as 2k 1 and i2k 1 ik ik i. sub exp_recurse use integer my bottom i j 1 @_ return i - 1 1 if j 0 return i if j 1 if j 2 Is j .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.