Algorithm Learning (14th), algorithm Learning

Source: Internet
Author: User

Algorithm Learning (14th), algorithm Learning
1. Fibonacci Divisibility

Note: Given a Fibonacci sequence, starting from 0 and 1,

0 1 1 2 3 5 8 13 21 34 ...

We recommend that you find the index of the first non-zero element in the list, which can be divisible by the value M. For example, if you give M = 17, the answer is 9 (index of element 34, 34 is divided by 17 ).

Input data:The first line contains the number of test cases.

The second row contains the divisor M factor (up to 10000 ).

Answer:It should contain indexes that meet the requirements of the Fibonacci series, separated by spaces.

For example:

input data:317 12 61answer:9 12 15

Test data:

187429 8478 3001 7204 9300 4803 4429 8703 2475 5778 3750 4126 9226 8926 6450 2327 9376 7579

The Code is as follows:

1 test_cases = int (input () 2 test_data = [int (num) for num in input (). split ()] 3 4 fib = [0, 1] # construct the fib Series 5 a, B = 0, 1 6 while B <10 ** 10000: 7, B = B, a + B 8 fib. append (B) 9 10 for n in test_data: 11 for m in fib [1:]: 12 if m % n = 0: 13 print (fib. index (m), end = '') 14 break # after finding the first number, disconnect 15 16 output: 72 2844 25 900 300 80 1144 264 300 36 7500 2064 7896 4464 3300 1246 1176 1890

 

2. Prime Ranges

Note: Given a pair of prime numbers (such as a and B), you need to find the total number of all prime numbers between them, including the given prime number.

isPrime(p) = true    anda <= p <= b

Input data:The first line contains the number of test pairs.

The following rows contain a pair of prime numbers. The first value is always smaller than the second one. All values must be less than 3 million.

Answer:It should include the number of prime numbers in the specified range of these pairs.

For example:

Input data: 3 5 19 # prime numbers between 5 and 19, including 5 and 19, with a total of 6 numbers 11 29 # prime numbers between 11 and 29, including 11 and 29, A total of 6 numbers 2 23 # similarly, there are 9 prime numbers between 2 and 23, including 2 and 23 answer: 6 6 9

Test data:

28
2528893 2601089
2017817 2070317
1528627 2299489
1988891 2260787
1604737 2311549
2020663 2070317
1739461 2397631
2397631 2528893
1464733 2286293
1325431 1854623
2339933 2447009
2571733 2679031
1545917 1722013
1464733 1504843
2309471 2397631
2469407 2601089
2309471 2447009
2240533 2625247
2578993 2601089
1585481 1737497
2311549 2563007
1570199 2723363
1782211 2629223
1916413 2266921
2095997 2352787
2421593 2497751
2237051 2562613
2562613 2705111

 

The Code is as follows:

Test_cases = int (input () def eladuosai (n): # irado filter method l = list (range (1, n + 1 )) l [0] = 0 for I in range (2, n + 1): if l [I-1]! = 0: for j in range (I * 2, n + 1, I): l [J-1] = 0 result = [x for x in l if x! = 0] return resultprime = eladuosai (3000000) # construct a list of prime numbers smaller than 3 million for _ in range (test_cases): data = [int (n) for n in input (). split ()] nums = [] for I in prime: if I <= data [0] <I + 1: # Find the index nums corresponding to the first value. append (prime. index (I) elif I <= data [1] <I + 1: # Find the index nums corresponding to the second value. append (prime. index (I) + 1) print (nums [1]-nums [0]), end = '') # The total number of indexes between the two indexes is calculated as follows: 4914 3610 53294 18681 48817 3417 45272 8905 56897 37035 7272 7269 12294 2840 5968 8951 9311 26140 1500 10583 17037 79161 57996 24080 17532 5153 22116 9674

 

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.