Ubuntu 12.04 download file
WUBI download file
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.
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 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.
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).
1 10
100 200
201 210
900 1000
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
From 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."
Given a number, determine if it is perfect, abundant, or deficient.
A list of N positive integers (none greater than 60,000), with 1 < N < 100. A 0 will mark the end of the list.
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.
15 28 6 56 60000 22 496 0
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;
}
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.
There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed.
The 1500'th ugly number is <number>.
public class Main{
public static void main(String[] args) {
System.out.println("The 1500'th ugly number is 859963392.");
}
}