# Mathematics Problem Solution

UVa Mathematics Problem Solution are more than easy to solve than Other UVa problem categories.Some ad hoc problems also included under this  categories.Most common mathematics problem are like: Number Systems or Sequences ,  Finding Prime Factors,Factorial,FibonacciNumbers,Prime Numbers, Cycle-Finding,GCD and/or LCM, Logarithm, Exponentiation, Power,and Other Number Theory Problems.

Normally most UVa Mathematics Problem Solution are easy and can be solved by previous knowledge.  But be careful about time, space and special criteria when solving them.

# UVa:  100 – The 3n + 1 problem

You have to Consider the following algorithm:

1.              input n

2.              print n

3.              if n = 1 then STOP

4.               if n is odd then n =  3n+1

5.               else n  =  n/2

6.              goto 2

Sample input

1 10
100 200
201 210
900 1000
Sample output

1 10 20
100 200 125
201 210 89
900 1000 174

Solution:

```#include <iostream>
using namespace std;
int main(void)
{
long i,j,m,t,k,max;
while(cin>>i>>j){
cout<<i<<" "<<j<<" ";
max=0;
if(i>j){
t=i;
i=j;
j=t;
}
for(m=i;m<=j;m++){
t=m;
k=0;
while(1){
if(t==1){
k++;
break;
}
k++;
if(t%2==0) t/=2;
else t=3*t+1;
}
if(k>max) max=k;
}
cout<<max;
}
return 0;
}```

# UVa:  324 – Factorial Frequencies

```import java.math.*;
import java.util.Scanner;

public class Main
{
public static void main (String [] args)
{
Scanner in = new Scanner (System.in);
BigInteger f[]= new BigInteger[400];
int i,n;
String s;

f[1]=BigInteger.ONE;
f[2]=BigInteger.valueOf(2);
for(i=3; i<=366; i++)
f[i]=BigInteger.valueOf(i).multiply(f[i-1]);

while(true)
{
n=in.nextInt();
if(n==0) break;
s=f[n].toString();
int ss[]=new int[10];
for(i=0; i<s.length(); i++)
ss[s.charAt(i)-48]++;

System.out.println(n+"! --");
System.out.printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d\n",ss[0],ss[1],ss[2],ss[3],ss[4]);
System.out.printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d\n",ss[5],ss[6],ss[7],ss[8],ss[9]);

}
}
}```

# UVa: 374 – Big Mod

```#include <stdio.h>

int bigmod(long long int b, long long int p, long long int m) {
if (p==0) return 1;
else if (p%2==0) return (bigmod((b%m*b%m)%m, p/2, m));
else return (b*bigmod(b, p-1, m))%m;
}

int main(){
long long int b , p , m;
while(scanf("%lld %lld %lld",&b,&p,&m)==3){
printf("%d\n",bigmod(b,p,m));
}
return 0;
}```

# UVa:  543 – Goldbach’s Conjecture

```#include<stdio.h>
#include<math.h>
#define max 1000000
int prime[max+10];

void prime_generator(){
unsigned long int i,j;
for(i=2; i<=sqrt(max); i++){
if(prime[i]==0)
for(j=2*i; j<=max; j+=i)
prime[j]=1;
}
}

int main(){
prime_generator();
unsigned long int n,i,j,x2,x1;

while(scanf("%lu",&n)==1){
if(n==0) break;
x1=0; x2=0;

for(i=n-3; i>=n/2; i--){
if(prime[i]==0){
if(prime[n-i]==0){
x1=i;
x2=n-i;
break;
}}
}
if(x1!=0&&x2!=0)
printf("%lu = %lu + %lu\n",n,x2,x1);
else
printf("Goldbach's conjecture is wrong.\n");
}
return 0;
}```

# UVa: 623 – 500!

```#include<stdio.h>
#define max 1001
#define len 2600
int num[max][len];
int fact()
{
int i,j;
num[0][0]=num[1][0]=1;
for(i=2; i<max; i++)
for(j=0; j<len; j++)
{
num[i][j]+=num[i-1][j]*i;
if(num[i][j]>9)
{
num[i][j+1]+=num[i][j]/10;
num[i][j]%=10;
}
}
return 0;
}
int main()
{
int n,i;
fact();
while(scanf("%d",&n)==1)
{
printf("%d!\n",n);
for(i=len-1; i>0; i--)
if(num[n][i]!=0)break;

for(;i>=0;i--)
printf("%d",num[n][i]);
printf("\n");
}
return 0;
}```
Updated: October 15, 2014 — 11:38 pm