search
HomeJavajavaTutorialSmith 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 if b="false;" d return public static void main args scanner s_c="new" system.out.print a number: int no="s_c.nextInt();" x="F_S_Digit(no);" y="F_Sum_P_Fact(no);" system.out.println of digit="+x);
System.out.println(" addition prime factors digits is="+y);
if(x==y)
System.out.print(" the user enterd number smith number. else system. out.print entered not>
<p><strong>Explanation</strong></p>
<p>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.</p>
<h4 id="Example">Example #2</h4>
<p>Let’s see another example as follows.</p>
<p><strong>Code:</strong></p>
<pre class="brush:php;toolbar:false">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  0)
{
s =s+(no % 10);
no = no/10;
}
return s;
}
public static void main(String args[])
{
for (int no = 1; no  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);
}
}
}
}</integer></integer>

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
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
Is Java Platform Independent if then how?Is Java Platform Independent if then how?May 09, 2025 am 12:11 AM

Java is platform-independent because of its "write once, run everywhere" design philosophy, which relies on Java virtual machines (JVMs) and bytecode. 1) Java code is compiled into bytecode, interpreted by the JVM or compiled on the fly locally. 2) Pay attention to library dependencies, performance differences and environment configuration. 3) Using standard libraries, cross-platform testing and version management is the best practice to ensure platform independence.

The Truth About Java's Platform Independence: Is It Really That Simple?The Truth About Java's Platform Independence: Is It Really That Simple?May 09, 2025 am 12:10 AM

Java'splatformindependenceisnotsimple;itinvolvescomplexities.1)JVMcompatibilitymustbeensuredacrossplatforms.2)Nativelibrariesandsystemcallsneedcarefulhandling.3)Dependenciesandlibrariesrequirecross-platformcompatibility.4)Performanceoptimizationacros

Java Platform Independence: Advantages for web applicationsJava Platform Independence: Advantages for web applicationsMay 09, 2025 am 12:08 AM

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce

JVM Explained: A Comprehensive Guide to the Java Virtual MachineJVM Explained: A Comprehensive Guide to the Java Virtual MachineMay 09, 2025 am 12:04 AM

TheJVMistheruntimeenvironmentforexecutingJavabytecode,crucialforJava's"writeonce,runanywhere"capability.Itmanagesmemory,executesthreads,andensuressecurity,makingitessentialforJavadeveloperstounderstandforefficientandrobustapplicationdevelop

Key Features of Java: Why It Remains a Top Programming LanguageKey Features of Java: Why It Remains a Top Programming LanguageMay 09, 2025 am 12:04 AM

Javaremainsatopchoicefordevelopersduetoitsplatformindependence,object-orienteddesign,strongtyping,automaticmemorymanagement,andcomprehensivestandardlibrary.ThesefeaturesmakeJavaversatileandpowerful,suitableforawiderangeofapplications,despitesomechall

Java Platform Independence: What does it mean for developers?Java Platform Independence: What does it mean for developers?May 08, 2025 am 12:27 AM

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

How to set up JVM for first usage?How to set up JVM for first usage?May 08, 2025 am 12:21 AM

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

How can I check Java platform independence for my product?How can I check Java platform independence for my product?May 08, 2025 am 12:12 AM

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version