0% found this document useful (0 votes)
5 views6 pages

Java Algorithms for Number Theory

The document contains multiple Java programs that implement various algorithms, including Booth's algorithm for multiplication, binary palindrome checking, Euler's phi function, strobogrammatic number identification, segmented sieve, and the basic sieve of Eratosthenes for prime number generation. Each program prompts the user for input and processes it to produce the desired output. The code snippets demonstrate basic programming concepts and algorithm implementations in Java.

Uploaded by

poojasammeta666
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Java Algorithms for Number Theory

The document contains multiple Java programs that implement various algorithms, including Booth's algorithm for multiplication, binary palindrome checking, Euler's phi function, strobogrammatic number identification, segmented sieve, and the basic sieve of Eratosthenes for prime number generation. Each program prompts the user for input and processes it to produce the desired output. The code snippets demonstrate basic programming concepts and algorithm implementations in Java.

Uploaded by

poojasammeta666
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BOOTH’S ALGORITHM:

import [Link].*;

import [Link].*;

public class Main{

public static void main(String args[]){

Scanner sc=new Scanner([Link]);

[Link]("ENTER 1st VALUE");

[Link]("ENTER 1st VALUE");

int a=[Link]();

int s=0;

int b=[Link]();

int len=[Link](a).length();

for(int i=0;i<=s;i++){

if((a&1)==1) s=s+b;

a=a>>1;

b=b<<1;

[Link](s);

BINARY PALINDROME:

import [Link].*;

import [Link].*;

public class Main{

public static void main(String args[]){

Scanner sc=new Scanner([Link]);


[Link]("Enter any value");

int n=[Link]();

String s1="";

String s2="";

while(n>0){

s1=n%2+s1;

s2=s2+n%2;

n=n/2;

if([Link](s2)){

[Link]("IT'S A BINARY NUMBER");

else {

[Link]("IT'S NOT A BINARY NUMBER");

EULER’S PHI:

import [Link].*;

import [Link].*;

public class Main{

static int gcd(int n1, int n2){

while(n1!=n2){

if(n1>n2) n1=n1-n2;

else n2=n2-n1;
}

return n2;

static int phi(int a){

int count=0;

int i;

for(i=1;i<=a;i++) {

if(gcd(i,a)==1) count++;

return count;

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

[Link]("Enter any value");

int n=[Link]();

for(int i=1;i<=n;i++) {

[Link]("phi("+i+")="+phi(i));

STROBOGRAMMATIC NUMBER :

import [Link].*;

public class Main{

public static void main(String args[]){

Scanner sc = new Scanner([Link]);

[Link]("Enter any value");

String s=[Link]();

String s1="";
for(int i=0;i<[Link]();i++){

char c=[Link](i);

if(c=='0')s1='0'+s1;

else if(c=='1')s1='1'+s1;

else if(c=='6')s1='9'+s1;

else if(c=='8')s1='8'+s1;

else if(c=='9')s1='6'+s1;

if([Link](s1)){

[Link]("STROBO");

else {

[Link]("NOT STROBO");

SEGMENETED SIEVE :

import [Link].*;

public class Main{

public static void main(String args[]){

Scanner sc = new Scanner([Link]);

[Link](" Enter 1st value");

[Link](" Enter 2nd value");

int l=[Link]();

int n=[Link]();

boolean b[] = new boolean[n+1];

for(int i=0;i<=n;i++) b[i] = true;

for(int i=2;i*i<=n;i++){
int s=(i/l)*l;

if(s<l) s=s+i;

for(int j=s;j<=n;j=j+i){

b[j]=false;

for(int i=1;i<=n;i++){

if (b[i]==true);

[Link](i+"");

SIEVE:

import [Link].*;

import [Link].*;

public class Main{

public static void main(String args[]){

Scanner sc = new Scanner([Link]);

int n=[Link]();

boolean b[] = new boolean[n+1];

for(int i=0;i<=n;i++) b[i]=true;

for(int i=2;i*i<=n;i++) {

for(int j=i*2;j<=n;j=j+i) b[j]=false;

for(int i=2;i<=n;i++){ if(b[i]==true) [Link](i + " ");

}
}

You might also like