Sat. Jul 13th, 2024

What is prime number:

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is a whole number greater than 1 that cannot be formed by multiplying two smaller natural numbers. For example, 2, 3, 5, 7, 11, and 13 are prime numbers because they cannot be divided evenly by any other number except 1 and themselves. However, 4 is not a prime number because it can be divided evenly by 1, 2, and 4

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter starting number"
android:inputType="number"
android:layout_margin="16dp"/>

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ending number"
android:inputType="number"
android:layout_below="@id/editText1"
android:layout_margin="16dp"/>

<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText2"
android:layout_centerHorizontal="true"
android:text="Calculate"/>

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/calculateButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</layout>

MainActivity.kt

package com.androindian.primenumber

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.androindian.primenumber.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
var biniding:ActivityMainBinding?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
biniding=DataBindingUtil.setContentView(this, R.layout.activity_main)

biniding?.calculateButton?.setOnClickListener {
val start = biniding?.editText1?.text.toString().toIntOrNull() ?: 0
val end = biniding?.editText2?.text.toString().toIntOrNull() ?: 0

biniding?.resultTextView?.text = findPrimes(start, end)
}
}

private fun isPrime(n: Int): Boolean {
if (n <= 1) return false
if (n <= 3) return true
if (n % 2 == 0 || n % 3 == 0) return false
var i = 5
while (i * i <= n) {
if (n % i == 0 || n % (i + 2) == 0) return false
i += 6
}
return true
}

private fun findPrimes(start: Int, end: Int): String {
val primes = StringBuilder()
for (i in start..end) {
if (isPrime(i)) {
primes.append("$i ")
}
}
return if (primes.isEmpty()) {
"No prime numbers found between $start and $end"
} else {
"Prime numbers between $start and $end: ${primes.toString().trim()}"
}
}
}

build.gradle

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.androindian.primenumber"
compileSdk = 34

defaultConfig {
applicationId = "com.androindian.primenumber"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
dataBinding{
enable=true
}
}

dependencies {

implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *