forked from sinagarajan/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonFactorForFBandRandom.java
More file actions
123 lines (85 loc) · 2.6 KB
/
Copy pathCommonFactorForFBandRandom.java
File metadata and controls
123 lines (85 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
Given a number K, find the smallest Fibonacci number that shares a common factor( other than 1 ) with it. A number is said to be a common factor of two numbers if it exactly divides both of them.
Output two separate numbers, F and D, where F is the smallest fibonacci number and D is the smallest number other than 1 which divides K and F.
Input Format
First line of the input contains an integer T, the number of testcases.
Then follows T lines, each containing an integer K.
Output Format
Output T lines, each containing the required answer for each corresponding testcase.
Sample Input
3
3
5
161
Sample Output
3 3
5 5
21 7
Explanation
There are three testcases. The first test case is 3, the smallest required fibonacci number 3. The second testcase is 5 and the third is 161. For 161 the smallest fibonacci numer sharing a common divisor with it is 21 and the smallest number other than 1 dividing 161 and 7 is 7.
Constraints :
1 <= T <= 5
2 <= K <= 1000,000
The required fibonacci number is guranteed to be less than 10^18.
Algorithm :
1. Find all the factors of the given number
2. For each fibonacci number that is generated , check if it is factor of any of the numbers obtained in steps 1
*/
/* JAVA PROGRAM */
import java.io.*;
public class Solution {
/* Compute final answer */
private static void findanswer(long data)
{
/* Initialization */
int sqroot= (int)Math.sqrt(data),flag,itr=0;
long[] array=new long[sqroot];
long div=2;
long temp,sum;
/* Find All the Factors of Random Number */
while(div <= data)
{
if( data % div == 0)
{
array[itr++]=div;
}
div++;
}
div=0;
sum=1;
flag=0;
/* Check for each fibonacci number , if it is divisble by the factors found in earlier step */
while(true)
{
temp=sum;
sum+=div;
div=temp;
for(int i=0;i<itr;i++)
{
if(sum % array[i] == 0)
{
System.out.println(sum+" "+array[i]);
flag=1;
break;
}
}
if(flag==1)break;
}
}
public static void main(String args[] ) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
long[] array=new long[N];
for (int i = 0; i < N; i++)
{
line = br.readLine();
array[i] = Long.parseLong(line);
}
for (int i = 0; i < N; i++)
{
findanswer(array[i]);
}
}
}