Experience in SiC 1.23-1.26

Source: Internet
Author: User

1.23 code modification is very simple. The key is time. On the computer, if all the values below 100000000 are 0, I thought the code was wrong.

In the end, there is no way to use 1e10 1e11 for calculation. It is found that the speed is between 1.2 and 1.5 faster than 1e11 1e12. Smaller than 2. After thinking about it for a long time, I cannot give a reasonable explanation.

At first, I thought it was slower to get the remainder of 3 5 7 than 4 6 8. After testing, I found that this is not the case. Some people on the internet suspect that function calls took some time to make if judgments. To be honest, the performance impact of this item should not be so great.

The only thing that comes to mind now is that the compiler has made some optimizations, resulting in performance not entirely dependent on the number of calls.

No reasonable analysis was found on the Internet.

1.24 Is also a pity, start to set the times to 10, the calculation time below 1e10 is completely 0. There is no way later, set the times to 10000, The results found that the time is also floating.

Sometimes, 1e12-1e13 is faster than 1e11-1e12. It can only be said that random causes random performance.

1.25 in theory, it must be right. It will be a tragedy in time. I will execute the range of 100000000 and 1000000000 here, and directly crash.

The root cause is that our CPU is 32 or 64-bit, not infinite.

1.26 there is a clear explanation of this topic's introduction to algorithms. Apply the formula T (n) = 2 T (n/2) in the introduction to algorithms to know that T (n) = theta (N)

To put it bluntly, we are not a good book on learning algorithms. It is not a good book about data structures.


The personal code is as follows:

1.23

(Define (search-for-Primes-new start end count)
(Define (timed-prime-Test N)
(Newline)
(Display n)
(START-prime-Test N (runtime )))
(Define (START-prime-Test N start-Time)
(If (Prime? N)
(Report-prime (-(runtime) Start-Time ))
0 ))
(Define (Report-prime elapsed-Time)
(Display "***")
(Display elapsed-Time)
1)
(Define (Prime? N)
(= N (smallest-divisor-new N )))
(Define (smallest-divisor-new N)
(Define (find-divisor n test-divisor)
(Cond (> (square test-divisor) n) N)
(Divides? Test-divisor N) Test-divisor)
(Else (find-divisor N (next test-divisor )))))
(Define (divides? A B)
(= (Remainder B A) 0 ))
(Define (next test-divisor)
(If (= test-divisor 2)
3
(+ Test-divisor 2 )))
(Find-divisor N 2 ))
(Define (search-iter start end count)
(If (or (> start end) (= count 0 ))
0
(If (= (timed-prime-Test start) 1)
(Search-ITER (+ start 1) end (-count 1 ))
(Search-ITER (+ start 1) end count ))))
(Search-iter start end count ))


1.24

(Define (search-for-Primes-new start end count)
(Define (timed-prime-Test N)
(Newline)
(Display n)
(START-prime-Test N (runtime )))
(Define (START-prime-Test N start-Time)
(If (fast-Prime? N 10000)
(Report-prime (-(runtime) Start-Time ))
0 ))
(Define (Report-prime elapsed-Time)
(Display "***")
(Display elapsed-Time)
1)
(Define (search-iter start end count)
(If (or (> start end) (= count 0 ))
0
(If (= (timed-prime-Test start) 1)
(Search-ITER (+ start 1) end (-count 1 ))
(Search-ITER (+ start 1) end count ))))
(Search-iter start end count ))
(Define (expmod base exp m)
(Cond (= exp 0) 1)
(Even? Exp)
(Remainder (square (expmod base (/Exp 2) M ))
M ))
(Else
(Remainder (* base (expmod base (-Exp 1) M ))
M ))))
(Define (fast-Prime? N times)
(Cond (= times 0) True)
(Fermat-Test N) (fast-Prime? N (-times 1 )))
(Else false )))
(Define (Fermat-Test N)
(Define (try-It)
(= (Expmod a n) ))
(Try-It (+ 1 (random (-N 1 )))))


1.25

(Define (search-for-Primes-25 start end count)
(Define (timed-prime-Test N)
(Newline)
(Display n)
(START-prime-Test N (runtime )))
(Define (START-prime-Test N start-Time)
(If (fast-Prime? N 10000)
(Report-prime (-(runtime) Start-Time ))
0 ))
(Define (Report-prime elapsed-Time)
(Display "***")
(Display elapsed-Time)
1)
(Define (search-iter start end count)
(If (or (> start end) (= count 0 ))
0
(If (= (timed-prime-Test start) 1)
(Search-ITER (+ start 1) end (-count 1 ))
(Search-ITER (+ start 1) end count ))))
(Search-iter start end count ))
(Define (expmod base exp m)
(Remainder (fast-expt base exp) m ))
(Define (fast-expt base exp)
(Cond (= exp 0) 1)
(Even? Exp)
(Square (fast-expt base (/Exp 2 ))))
(Else
(* Base (fast-expt base (-Exp 1 ))))))
(Define (fast-Prime? N times)
(Cond (= times 0) True)
(Fermat-Test N) (fast-Prime? N (-times 1 )))
(Else false )))
(Define (Fermat-Test N)
(Define (try-It)
(= (Expmod a n) ))
(Try-It (+ 1 (random (-N 1 )))))

Experience in SiC 1.23-1.26

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.