亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Logic behind smith number
How to Check Smith number in Java?
Examples
Example #2
Conclusion
Home Java javaTutorial Smith Number in Java

Smith Number in Java

Aug 30, 2024 pm 04:28 PM
java

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.

Smith Number in Java

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Grow A Garden - All Animals And How To Get Them
4 weeks ago By 尊渡假賭尊渡假賭尊渡假賭
How to Remove & Clean Ink in Cash Cleaner Simulator
3 weeks ago By 尊渡假賭尊渡假賭尊渡假賭
Roblox: Grow A Garden - Complete Weather Guide
1 months ago By 尊渡假賭尊渡假賭尊渡假賭
Revenge Of The Savage Planet: Every Outfit And How To Unlock It
3 weeks ago By 尊渡假賭尊渡假賭尊渡假賭

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JavaScript to determine whether two arrays are equal? How to use JavaScript to determine whether two arrays are equal? May 23, 2025 pm 10:51 PM

In JavaScript, you need to use a custom function to determine whether two arrays are equal, because there is no built-in method. 1) Basic implementation is to compare lengths and elements, but cannot process objects and arrays. 2) Recursive depth comparison can handle nested structures, but requires special treatment of NaN. 3) Special types such as functions and dates need to be considered, and further optimization and testing are required.

Use VSCode to perform version fallback operation of code Use VSCode to perform version fallback operation of code May 15, 2025 pm 09:42 PM

In VSCode, you can use Git for code version fallback. 1. Use gitreset--hardHEAD~1 to fall back to the previous version. 2. Use gitreset--hard to fall back to a specific commit. 3. Use gitrevert to safely fall back without changing history.

How to correctly handle this pointing in a closure? How to correctly handle this pointing in a closure? May 21, 2025 pm 09:15 PM

The methods to correctly handle this pointing in JavaScript closures include: 1. Use arrow functions, 2. Use bind methods, 3. Use variables to save this. These methods ensure that this intrinsic function correctly points to the context of the external function.

How to implement data encryption with JavaScript? How to implement data encryption with JavaScript? May 23, 2025 pm 11:12 PM

Using JavaScript to implement data encryption can use the Crypto-JS library. 1. Install and introduce the Crypto-JS library. 2. Use the AES algorithm for encryption and decryption to ensure that the same key is used. 3. Pay attention to the secure storage and transmission of keys. It is recommended to use CBC mode and environment variables to store keys. 4. Consider using WebWorkers when you need high performance. 5. When processing non-ASCII characters, you need to specify the encoding method.

What are the four categories of Java? Description of Java Basic Type System Classification What are the four categories of Java? Description of Java Basic Type System Classification May 20, 2025 pm 08:27 PM

Java's four basic type systems include integer types, floating point types, character types and boolean types. 1. Integer types (byte, short, int, long) are used to store numerical values ??without decimals. Choosing the appropriate type can optimize memory and performance. 2. Float type (float, double) is used for decimal values. Pay attention to accuracy issues. If necessary, BigDecimal is used. 3. Character type (char) is based on Unicode and is suitable for single characters, but String may be required in international applications. 4. Boolean types are used for true and false values, simplifying logical judgments and improving code readability.

Python concatenate list strings Python concatenate list strings May 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Environment configuration for running Ruby code in VSCode Environment configuration for running Ruby code in VSCode May 15, 2025 pm 09:30 PM

Configuring the Ruby development environment in VSCode requires the following steps: 1. Install Ruby: Download and install from the official website or using RubyInstaller. 2. Install the plug-in: Install CodeRunner and Ruby plug-ins in VSCode. 3. Set up the debugging environment: Install the DebuggerforRuby plug-in and create a launch.json file in the .vscode folder for configuration. This way, you can write, run, and debug Ruby code efficiently in VSCode.

How to copy and paste layer styles in PS? How to copy and paste layer styles in PS? May 16, 2025 am 06:00 AM

Copying and pasting layer styles in Photoshop is a key trick to improve productivity. Let's dive into how to do it, and the various details and techniques you may encounter in the process. When we talk about copying and pasting layer styles in Photoshop, the first thing we need to understand is that layer styles refer to effects applied to layers, such as shadows, glows, bevels, and reliefs. Mastering this feature not only saves time, but also ensures consistent design. To copy the style of a layer, right-click the layer you want to copy and select Copy Layer Style. This will copy all the styles of the layer into the clipboard. Next, select the target layer you want to apply these styles, right-click it, and select Paste

See all articles