Chapter 4 exercise: fepona. Qi series & vampire numbers

Source: Internet
Author: User
Package COM. twoslow. cha4;/*** Fibonacci sequence: Starting from three numbers, each number is the sum of the first two: 1 1 2 3 5 8 13... * @ author sai **/public class Fibonacci {public static void main (string [] ARGs) {for (INT I = 0; I <10; I ++) {system. out. print (FIB (I) + ",") ;}} public static int fib (INT N) {If (n <2) return 1; return fib (n-1) + fib (n-2 );}}
Package COM. twoslow. cha4;/*** vampire number: a number with an even number, which can be multiplied by a number. * This number contains a half-digit number, the number * selected from the initial number can be sorted in any order. Numbers ending with two zeros are not allowed. * Eg: 1260 = 21*60; 1827 = 21*87; 2187 = 27*81 * Find all 4-digit vampire numbers. * @ Author sai **/public class vampirenumbers2 {static int A (int I) {// return I/1000;} static int B (int I) {// hundred bits return (I % 1000)/100;} static int C (int I) {// ten return (I % 1000) % 100)/10 ;} static int D (int I) {// return (I % 1000) % 100) % 10;} static int COM (int I, Int J) {return (I * 10) + J;} static void producttest (int I, int M, int N) {If (M * n = I) system. out. println (I + "=" + M + "*" + n);} public static void main (string [] ARGs) {long start = system. currenttimemillis (); For (INT I = 1001; I <9999; I ++) {// The permutation and combination of any number of 2 numbers from 4 is equal to 4 × 3 = 12 producttest (I, COM (a (I), B (I )), COM (C (I), D (I); producttest (I, COM (a (I), B (I), COM (d (I ), C (I); producttest (I, COM (a (I), C (I), COM (B (I), D (I ))); producttest (I, COM (a (I), C (I), COM (d (I), B (I); producttest (I, COM (a (I), D (I), COM (B (I), C (I); producttest (I, COM (a (I ), d (I), COM (C (I), B (I); producttest (I, COM (B (I), A (I )), COM (C (I), D (I); producttest (I, COM (B (I), A (I), COM (d (I ), C (I); producttest (I, COM (B (I), C (I), COM (d (I), A (I ))); producttest (I, COM (B (I), D (I), COM (C (I), A (I); producttest (I, COM (C (I), A (I), COM (d (I), B (I); producttest (I, COM (C (I ), B (I), COM (d (I), A (I);} long end = system. currenttimemillis (); system. out. println (end-Start); // simple test: The time is between 22 and 24, and 22 mostly }}
Package COM. twoslow. cha4; import Java. util. arrays;/*** vampire number: a number with an even number, which can be multiplied by a number. * This number contains a half-digit number, the number * selected from the initial number can be sorted in any order. Numbers ending with two zeros are not allowed. * Eg: 1260 = 21*60; 1827 = 21*87; 2187 = 27*81 * Find all 4-digit vampire numbers. * Result: 1260 = 21*60 1395 = 15*93 1435 = 41*35 1530 = 51*30 1827 = 87*21 2187 = 27*81 6880 = 86*80 6880 = 80*86 * @ author sai **/public class vampirenumbers3 {public static void main (string [] ARGs) {long start = system. currenttimemillis (); int I _val; char [] C1, C2; For (INT x = 10; x <100; X ++) {for (INT y = x + 1; Y <100; y ++) {I _val = x * Y; If (I _val % 100 = 0 & (I _val-x -Y) % 9! = 0) {continue;} C1 = string. valueof (I _val ). tochararray (); C2 = (string. valueof (x) + String. valueof (y )). tochararray (); arrays. sort (C1); arrays. sort (C2); If (arrays. equals (C1, C2) {system. out. println (I _val + "=" + x + "*" + Y) ;}} long end = system. currenttimemillis (); system. out. println (end-Start); // simple test: 31 32 mostly }}
Package COM. twoslow. cha4; import Java. util. arrays;/*** vampire number: a number with an even number, which can be multiplied by a number. * This number contains a half-digit number, the number * selected from the initial number can be sorted in any order. Numbers ending with two zeros are not allowed. * Eg: 1260 = 21*60; 1827 = 21*87; 2187 = 27*81 * Find all 4-digit vampire numbers. * Result: 1260 = 21*60 1395 = 15*93 1435 = 41*35 1530 = 51*30 1827 = 87*21 2187 = 27*81 6880 = 86*80 6880 = 80*86 * @ author sai **/public class vampirenumbers {public static void main (string [] ARGs) {long start = system. currenttimemillis (); string [] ar_str1, ar_str2; int sum = 0; int from; int to; int I _val; int COUNT = 0; // double loop for (INT I = 10; I <100; I ++) {// J = I + 1 to avoid repetition Not quite familiar with from &. If there are already 14 groups, from = math. max (1000/I, I + 1); To = math. min (10000/I, 100); For (Int J = from; j <to; j ++) {I _val = I * J; /** I _val % 100 = 0: exclude the ending number of 2 0 * I _val-i-j % 9! = 0: Any four digits can be considered as: I _val = 1000a + 100b + 10C + D * any two digits can be composed of any two of the four digits. For example, 10a + B, 10B + A, 10C + ..., we can see that the coefficient is 10 or 1 * I _val-I-j = 1000a + 100b + 10C + d-m1 * a-m2 * b-m3 * c-m4 * D * = (1000-m1) * A + (100-m2) * B + (10-m3) * C + (1-m4) * D * Whether M1, M2, M3, or M4 is 1 or 10, a, B, c, d. The coefficients of the four numbers are multiples of 9. * **/If (I _val % 100 = 0 | (I _val-I-j) % 9! = 0) {continue;} count ++; ar_str1 = string. valueof (I _val ). split (""); // If I _val = "1260", ar_str1 = {"", "1", "2", "6 ", "0"} ar_str2 = (string. valueof (I) + String. valueof (j )). split (""); arrays. sort (ar_str1); arrays. sort (ar_str2);/** sort to determine whether all four numbers are the same */If (arrays. equals (ar_str1, ar_str2) {// compare after sorting. If it is true, a set of sum ++; system is found. out. println ("Number" + sum + "group:" + I + "*" + J + "=" + I _val) ;}} long end = system. currenttimemillis (); system. out. println (end-Start); system. out. println ("found" + sum + "Number of vampire groups"); system. out. println (count); // simple test: the time range is 17-20, 18 and 19 }}

 

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.