Saturday, December 30, 2017
Thursday, September 17, 2015
UVA-100 - The 3n + 1 problem Using Java and C
100 - The 3n + 1 problem
Time limit: 3.000 seconds
Background
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.
The Problem
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then![]()
5. else![]()
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
The Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and includingi and j.
You can assume that no operation overflows a 32-bit integer.
The Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
Using Java
import java.util.Scanner;
public class Main{
static long cou(long n)
{
if (n==1)
return 1;
else if(n%2!=0)
{
return cou(3*n+1)+1;
}
else
{
return cou(n/2)+1;
}
}
static long abc(long a,long b)
{
if(b<a)
{
return abc( b, a);
}
long count=0,ln;
while(a<=b)
{
ln=cou(a);
if(ln>count)
{
count=ln;
}
a++;
}
return count;
}
public static void main(String[] args) {
long a,b;
Scanner s=new Scanner(System.in);
while(s.hasNext())
{
a=s.nextLong();
b=s.nextLong();
System.out.println(a+" "+b+" "+abc(a,b));
}
}
}
Using C
#include<stdio.h>
long long cou(long long n)
{
if (n==1)
return 1;
else if(n%2!=0)
{
return cou(3*n+1)+1;
}
else
{
return cou(n/2)+1;
}
}
long long abc(long long a,long long b)
{
if(b<a)
{
return abc( b, a);
}
long long count=0,ln;
while(a<=b)
{
ln=cou(a);
if(ln>count)
{
count=ln;
}
a++;
}
return count;
}
int main()
{
long long a,b;
while(scanf("%lld%lld",&a,&b)!=EOF)
{
printf("%lld %lld %lld\n",a,b,abc(a,b));
}
return 0;
}
UVA-382–Perfection- Using java And C
UVA-382–Perfection- Using java
382 - Perfection
Time limit: 3.000 secondsFrom the article Number Theory in the 1994 Microsoft Encarta: ``If a, b, c are integers such that a = bc, a is called a multiple of b or ofc, and b or c is called a divisor or factor of a. If c is not , b is called a proper divisor of a. Even integers, which include 0, are multiples of 2, for example, -4, 0, 2, 10; an odd integer is an integer that is not even, for example, -5, 1, 3, 9. A perfect number is a positive integer that is equal to the sum of all its positive, proper divisors; for example, 6, which equals 1 + 2 + 3, and 28, which equals 1 + 2 + 4 + 7 + 14, are perfect numbers. A positive number that is not perfect is imperfect and is deficient or abundant according to whether the sum of its positive, proper divisors is smaller or larger than the number itself. Thus, 9, with proper divisors 1, 3, is deficient; 12, with proper divisors 1, 2, 3, 4, 6, is abundant."
Problem Statement
Given a number, determine if it is perfect, abundant, or deficient.
Input
A list of N positive integers (none greater than 60,000), with 1 < N < 100. A 0 will mark the end of the list.
Output
The first line of output should read PERFECTION OUTPUT. The next N lines of output should list for each input integer whether it is perfect, deficient, or abundant, as shown in the example below. Format counts: the echoed integers should be right justified within the first 5 spaces of the output line, followed by two blank spaces, followed by the description of the integer. The final line of output should read END OF OUTPUT.
Sample Input
15 28 6 56 60000 22 496 0
Sample Output
PERFECTION OUTPUT
15 DEFICIENT
28 PERFECT
6 PERFECT
56 ABUNDANT
60000 ABUNDANT
22 DEFICIENT
496 PERFECT
END OF OUTPUT
Using Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a;
System.out.println("PERFECTION OUTPUT");
Scanner s=new Scanner(System.in);
while(s.hasNext())
{
a=s.nextInt();
if(a==0)
{
System.out.println("END OF OUTPUT");
break;
}
int n=a,i,sum=0;
for(i=1;i<a;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==a)
{
System.out.format("%5d PERFECT\n", a);
}
else if(sum>a)
{
System.out.format("%5d ABUNDANT\n", a);
}
else if(sum<a)
{
System.out.format("%5d DEFICIENT\n", a);
}
}
}
}
Using C
#include<stdio.h>
int main()
{
int a;
printf("PERFECTION OUTPUT\n");
while(scanf("%d",&a)!=EOF)
{
if(a==0)
{
printf("END OF OUTPUT\n");
break;
}
int n=a,i,sum=0;
for(i=1;i<a;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==a)
{
printf("%5d PERFECT\n",a);
}
else if(sum>a)
{
printf("%5d ABUNDANT\n",a);
}
else if(sum<a)
{
printf("%5d DEFICIENT\n",a);
}
}
return 0;
}
Monday, November 24, 2014
UVA - 136 - Ugly Numbers
136 - Ugly Numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500'th ugly number.
Input and Output
There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed.
Sample output
The 1500'th ugly number is <number>.
#include<stdio.h>
int main()
{
printf("The 1500'th ugly number is 859963392.\n");
return 0;
}
11479 - Is this the easiest problem
11479 - Is this the easiest problem
A triangle is a geometric shape with three positive sides. However, any given three sides won't neces-
sarily form a triangle. The three sides must form a closed region. Triangles are categorized depending
on the values of the sides of a valid triangle. In this problem you are required to determine the type of
a triangle.
Input
The rst line of input will contain a positive integer T < 20, where T denotes the number of test cases.
Each of the next T lines will contain three 32 bit signed integer.
Output
For each case of input there will be one line of output. It will be formatted as:
Case x: triangle type.
Where x denotes the case number being processed and triangle type is the type of the triangle.
triangle type will be one of the following, depending on the values of the three sides:
Invalid - The three sides can not form a triangle
Equilateral - All three sides of valid triangle are equal
Isosceles - Exactly two of the sides of a valid triangle are equal.
Scalene - No pair of sides are equal in a valid triangle.
Sample Input
4
1 2 5
1 1 1
4 4 2
3 4 5
Sample Output
Case 1: Invalid
Case 2: Equilateral
Case 3: Isosceles
Case 4: Scalene
#include<stdio.h>
int main()
{
int a,b,i=1,c,T,n;
scanf("%d",&T);
while(T>0)
{
scanf("%d%d%d",&a,&b,&c);
if((a==b)&&(b==c))
{
printf("Case %d: Equilateral\n",i);
}
else if((a+b)<=c || (b+c)<=a || (c+a)<=b)
printf("Case %d: Invalid\n",i);
else if(a<=0 || b<=0 || c<=0)
printf("Case %d: Invalid\n",i);
else if(a==b || b==c)
{
printf("Case %d: Isosceles\n",i);
}
else
{
printf("Case %d: Scalene\n",i);
}
T--;
i++;
}
return 0;
}