Some special Java numeric Algorithms

Source: Internet
Author: User
1 Package Specialnumber;
2
3 Import Java. util. arraylist;
4 Import Java. util. arrays;
5 Import Java. util. hashset;
6 Import Java. util. iterator;
7 Import Java. util. List;
8 Import Java. util. Set;
9
10 Public Class Sushu {
11
12 Public Static Void Main (string [] ARGs ){
13 List <integer> result = New Arraylist <integer> ();
14 For ( Int I = 1; I <100; I ++ ){
15 If (Iswanshu (I ))
16 Result. Add (I );
17 }
18 System. Out. println (result. Size ());
19 System. Out. println (arrays. tostring (result. toarray ()));
20 }
21
22 /**
23 * Prime number (prime number)
24 */
25 Public Static Boolean Issushu ( Int Number ){
26 Boolean Flag = True ;
27 // Int mid = (INT) math. SQRT (number );
28 For ( Int J = 2; j * j <= number; j ++ ){
29 If (Number % J = 0 ){
30 Flag = False ;
31 Break ;
32 }
33 }
34 Return Flag;
35 }
36
37 /**
38 * Quantity
39 */
40 Public Static Boolean Iswanshu ( Int Number ){
41 Int Mid = ( Int ) Math. SQRT (number );
42 Int Sum = 1;
43 Set <integer> set = New Hashset <integer> ();
44 For ( Int I = 2; I <= mid; I ++ ){
45 If (Number % I = 0 ){
46 Set. Add (I );
47 Set. Add (number/I );
48 }
49 }
50 Iterator <integer> it = set. iterator ();
51 While (It. hasnext ()){
52 Int Num = (integer) it. Next ();
53 Sum + = num;
54 }
55 Return Sum = number;
56 }
57
58 /**
59 * Daffodils
60 */
61 Public Static Boolean Isshuixianhua ( Int Number ){
62 Char [] C = ("" + number). tochararray ();
63 Int Sum = 0;
64 For ( Int I = 0; I <C. length; I ++ ){
65 Sum + = math. Pow (integer. parseint (string. valueof (C [I]), 3 );
66 }
67 Return Sum = number;
68 }
69
70 /**
71 * Square number of replies
72 */
73 Public Static Boolean Ispingfanghuiwen ( Int Number ){
74 Boolean Flag = True ;
75 String STR = Number + "";
76 Char [] C = Str. tochararray ();
77 Int Length = Str. Length ();
78 For ( Int J = 0; j <= length/2; j ++ ){
79 If (C [J]! = C [length-1-J]) {
80 Flag = False ;
81 Break ;
82 }
83 }
84 Return Flag & (math. SQRT (number) % 1 = 0 );
85 }
86

87} I was a little idle yesterday and sorted out some special numbersAlgorithmMake a note.

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.