加快青岛公卫建设:每百张床位配一名公卫医师
In Java we have a different kind of functionality; the smith number is the one kind of functionality that is provided to the user. Basically, the smith number is nothing but the composite number (whose base is 10 in the number system) whose addition of all digits is equal to the addition of all digits of its all prime factor (excluding 1), this is known as the smith number. Another name for smith number is the joke number. By definition, we can say that all prime numbers are excluded naturally if they satisfy the condition. Normally smith number is a very basic subfield of the number system used in mathematics as per user requirement and some of its requirement of the problem statement.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Logic behind smith number
Now let’s see what logic behind the smith number is as follows.
Logic of the smith number is very simple, let’s consider the following number then we will easily understand the logic as follows.
Example: Suppose we need to check if the given number is a smith number or not.
The given number is: 95
First, we need to find out the primes factor of 95 is 19 and 5 (5, 19)
Now find the sum of digit that is 9 + 5 = 14
Sum of prime factor is 5 + 1 + 9 = 15
Now compare both results, see here 14 is not equal to 15. So the given number is not a prime number.
Let’s consider another number as follows.
Given number: 58
First find the primes factor of 58 = 2 and 29
Sum of prime factor is = 2 + 2 +9 = 13
Sum of given number digit is = 5 + 8 = 13
Now compare both results, here both results are same or we can say that is equal. So we can say that the given number is the smith number.
So this is the very simple logic behind the smith number, we just need to compare the prime factorial sum and digit sum. If both sums are equal then the given number is the smith number otherwise the number is not a smith number.
How to Check Smith number in Java?
Now let’s see how we can check if a given number is smith or not in java as follows.
In the above point, we already discussed different examples of smith numbers. Now let’s see the different steps to find the smith number as follows.
1. First we need to initialize or read numbers from the user.
2. After that we need to find the sum of the digits of the given number.
3. Next we need to find the primes factor of a given number.
4. Now calculate the sum of digits of prime factors.
5. Now compare the sum of digits of a given number and sum of digits of prime factors.
a. If both sums are equal then we can say that the given number is the smith number.
b. Else, we can consider a given number is not a smith number because sums are different.
So the above-mentioned steps are useful to implement smith number programs in Java.
Examples
Now let’s see the different examples of smith numbers in java for better understanding as follows.
Example #1
Code:
import java.util.*; public class Smith_Num { static int F_Sum_P_Fact(int no) { int j=2, add=0; while(no>1) { if(no%j==0) { add=add+F_S_Digit(j); no=no/j; } else { do { j++; } while(!isPrime(j)); } } return add; } static int F_S_Digit(int no) { int sum=0; while(no>0) { sum=sum+no%10; no=no/10; } return sum; } static boolean isPrime(int j) { boolean b=true; int d=2; while(d<Math.sqrt(j)) { if(j%d==0) { b=false; } d++; } return b; } public static void main(String args[]) { Scanner s_c = new Scanner(System.in); System.out.print("Enter a number: "); int no=s_c.nextInt(); int x = F_S_Digit(no); int y = F_Sum_P_Fact(no); System.out.println("addition of digit = "+x); System.out.println("addition of prime factors digits is = "+y); if(x==y) System.out.print("The user enterd number is smith number."); else System. out.print("The user entered number is not smith number."); } }
Explanation
In the above program, we try to implement the smith number program in java. Here we first created the function to sum of digits of prime factors; similarly, we also created the function to find the sum of digits for the given number as shown in the above program. After that, we created the function to check if the given number is a prime number or not by using Boolean functions. Then we write the main function, inside the main function we accept the number from the user and call all function which we already created and compare both sums. If the sum is equal then print the given number as smith number and if the sum is not equal then print the given number is not smith number. The final output of the above program we illustrate by using the following screenshot as follows.
Example #2
Let’s see another example as follows.
Code:
import java.util.*; public class Smith_Num_2 { static List<Integer> F_P_Fact(int no) { List<Integer> output = new ArrayList<>(); for (int j = 2; no % j == 0; no = no/j) output.add(j); for (int j = 3; j* j <= no; j=j+2) { while (no % j == 0) { output.add(j); no = no/j; } } if (no != 1) output.add(no); return output; } static int S_Digit(int no) { int s= 0; while (no > 0) { s =s+(no % 10); no = no/10; } return s; } public static void main(String args[]) { for (int no = 1; no < 5000; no++) { List<Integer> Fact = F_P_Fact(no); if (Fact.size() > 1) { int s = S_Digit(no); for (int fa : Fact) s =s-S_Digit(fa); if (s == 0) System.out.println(no); } } } }
Explanation
In the above example, we try to find out the all smith number up to 5000 as shown. The final output of the above program we illustrate by using the following screenshot as follows.
Conclusion
We hope from this article you learn Smith Number in java. From the above article, we have learned the basic logic of Smith Number and we also see different examples of Smith Number. From this article, we learned how and when we use the Smith Number in java.
The above is the detailed content of Smith Number in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

First, use JavaScript to obtain the user system preferences and locally stored theme settings, and initialize the page theme; 1. The HTML structure contains a button to trigger topic switching; 2. CSS uses: root to define bright theme variables, .dark-mode class defines dark theme variables, and applies these variables through var(); 3. JavaScript detects prefers-color-scheme and reads localStorage to determine the initial theme; 4. Switch the dark-mode class on the html element when clicking the button, and saves the current state to localStorage; 5. All color changes are accompanied by 0.3 seconds transition animation to enhance the user

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

Yes, a common CSS drop-down menu can be implemented through pure HTML and CSS without JavaScript. 1. Use nested ul and li to build a menu structure; 2. Use the:hover pseudo-class to control the display and hiding of pull-down content; 3. Set position:relative for parent li, and the submenu is positioned using position:absolute; 4. The submenu defaults to display:none, which becomes display:block when hovered; 5. Multi-level pull-down can be achieved through nesting, combined with transition, and add fade-in animations, and adapted to mobile terminals with media queries. The entire solution is simple and does not require JavaScript support, which is suitable for large

Full screen layout can be achieved using Flexbox or Grid. The core is to make the minimum height of the page the viewport height (min-height:100vh); 2. Use flex:1 or grid-template-rows:auto1frauto to make the content area occupy the remaining space; 3. Set box-sizing:border-box to ensure that the margin does not exceed the container; 4. Optimize the mobile experience with responsive media query; this solution is compatible with good structure and is suitable for login pages, dashboards and other scenarios, and finally realizes a full screen page layout with vertical centering and full viewport.

Selecting the Java SpringBoot React technology stack can build stable and efficient full-stack web applications, suitable for small and medium-sized to large enterprise-level systems. 2. The backend uses SpringBoot to quickly build RESTfulAPI. The core components include SpringWeb, SpringDataJPA, SpringSecurity, Lombok and Swagger. The front-end separation is achieved through @RestController returning JSON data. 3. The front-end uses React (in conjunction with Vite or CreateReactApp) to develop a responsive interface, uses Axios to call the back-end API, and ReactRouter

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

Use performance analysis tools to locate bottlenecks, use VisualVM or JProfiler in the development and testing stage, and give priority to Async-Profiler in the production environment; 2. Reduce object creation, reuse objects, use StringBuilder to replace string splicing, and select appropriate GC strategies; 3. Optimize collection usage, select and preset initial capacity according to the scene; 4. Optimize concurrency, use concurrent collections, reduce lock granularity, and set thread pool reasonably; 5. Tune JVM parameters, set reasonable heap size and low-latency garbage collector and enable GC logs; 6. Avoid reflection at the code level, replace wrapper classes with basic types, delay initialization, and use final and static; 7. Continuous performance testing and monitoring, combined with JMH

fixture is a function used to provide preset environment or data for tests. 1. Use the @pytest.fixture decorator to define fixture; 2. Inject fixture in parameter form in the test function; 3. Execute setup before yield, and then teardown; 4. Control scope through scope parameters, such as function, module, etc.; 5. Place the shared fixture in conftest.py to achieve cross-file sharing, thereby improving the maintainability and reusability of tests.
