Finding the squares up to 10,000,000 in C - c

The sum of squares of the 3 consecutive numbers 11, 12 and 13 is 434 (that is 121 + 144 + 169 = 434). The number 434 reads the same from both ways and is called a palindrome. I need to find out the sum of the numbers less than 10^7 that can be expressed as the sum of consecutive squares and results in a
palindrome. If in 2 different sequences, a number repeats, then sum
them twice. That is if 11 occurs in 2 consecutive number sequences, sum it twice.
I need to write a program based on the above scenario.
What I understood is we have to find squares up to 10,000,000 and then all the numbers. How should I approach writing a program to do this in C?

You probably need a for loop which increments a variable? Using this variable you can generate 3 consecutive numbers.. then sum up the squared numbers.. if it's above your max number you stop the loop. if it's below you check whether it's a palindrom?

Using brute force way is one such possible way.
Iterate a variable i from 1 to 10^7 - 2 so that you are going to take sum of squares of first three value of variable (including i) and find whether its palindrome or not.
ie. when i=5, in a for loop
you need to find whether i^2 + (i+1)^2 + (i+2)^2 is palindrome or not.
I am not sure but you rather take long long as you need to calculate squares.

Related

Maximum average distance between two numbers across multiple arrays

Let's say you have k arrays of size N, each containing unique values from 1 to N.
How would you find the two numbers that are on average the furthest away from each other?
For example, given the arrays:
[1,4,2,3]
[4,2,3,1]
[2,3,4,1]
Then the answer would be item 1 and 2, because they are of distance 2 apart in the first two arrays, and 3 numbers apart in the last one.
I am aware of an O(kN^2) solution (by measuring the distance between each pair of numbers for each of the k arrays), but is there a better solution?
I want to implement such an algorithm in C++, but any description of a solution would be helpful.
After a linear-time transformation indexing the numbers, this problem boils down to computing the diameter of a set of points with respect to L1 distance. Unfortunately this problem is subject to the curse of dimensionality.
Given
1 2 3 4
1: [1,4,2,3]
2: [4,2,3,1]
3: [2,3,4,1]
we compute
1 2 3
1: [1,4,4]
2: [3,2,1]
3: [4,3,2]
4: [2,1,3]
and then the L1 distance between 1 and 2 is |1-3| + |4-2| + |4-1| = 8, which is their average distance (in problem terms) times k = 3.
That being said, you can apply an approximate nearest neighbor algorithm using the input above as the database and the image of each point in the database under N+1-v as a query.
I've a suggestion for the best case. You can follow an heuristical approach.
For instance, You know that if N=4, N-1=3 will be the maximum distance and 1 will be the minimum. The mean distance is 10/6=1,66667 (sums of distances among pairs within array / number of pairs within an array).
Then, you know that if two numbers are on the edges for k/2 arrays (most of the times), it is already on the average top (>= 2 of distance), even if they're just 1 distance apart in the other k/2 arrays. That could be a solution for a best case in O(2k) = O(k).

In C language,How to print all binary numbers of size N without 2 consecutive 0's with recursion

How to print all binary numbers of size N without 2 consecutive 0's with recursion?
without using arrays at all,nor loops.
and they need to be printed by order like.
for example:
N=3:
010
011
101
110
111
There are two different questions here. First is algorithmic: how to perform the task recursively independently of any language or constraint. Simple:
the recursive function takes a sequence of 0 or 1
if the length of the sequence is N, just print it and return
else if last element is a 1 recurse two times, once by adding a 0 to the sequence, once by adding a 1
else recurse only once by adding a 1
Just call that function with a singleton 0 and a singleton 1 and you are done.
The second part is the C implementation. You can use an int (provided N is not greater than the number of bits in an int) and use bit shifting, keeping the number of bits set to store a sequence. The actual implementation if left as an exercise...

Determine the adjacency of two fibonacci number

I have many fibonacci numbers, if I want to determine whether two fibonacci number are adjacent or not, one basic approach is as follows:
Get the index of the first fibonacci number, say i1
Get the index of the second fibonacci number, say i2
Get the absolute value of i1-i2, that is |i1-i2|
If the value is 1, then return true.
else return false.
In the first step and the second step, it may need many comparisons to get the correct index by using accessing an array.
In the third step, it need one subtraction and one absolute operation.
I want to know whether there exists another approach to quickly to determine the adjacency of the fibonacci numbers.
I don't care whether this question could be solved mathematically or by any hacking techniques.
If anyone have some idea, please let me know. Thanks a lot!
No need to find the index of both number.
Given that the two number belongs to Fibonacci series, if their difference is greater than the min. number among them then those two are not adjacent. Other wise they are.
Because Fibonacci series follows following rule:
F(n) = F(n-1) + F(n-2) where F(n)>F(n-1)>F(n-2).
So F(n) - F(n-1) = F(n-2) ,
=> Diff(n,n-1) < F(n-1) < F(n-k) for k >= 1
Difference between two adjacent fibonaci number will always be less than the min number among them.
NOTE : This will only hold if numbers belong to Fibonacci series.
Simply calculate the difference between them. If it is smaller than the smaller of the 2 numbers they are adjacent, If it is bigger, they are not.
Each triplet in the Fibonacci sequence a, b, c conforms to the rule
c = a + b
So for every pair of adjacent Fibonaccis (x, y), the difference between them (y-x) is equal to the value of the previous Fibonacci, which of course must be less than x.
If 2 Fibonaccis, say (x, z) are not adjacent, then their difference must be greater than the smaller of the two. At minimum, (if they are one Fibonacci apart) the difference would be equal to the Fibonacci between them, (which is of course greater than the smaller of the two numbers).
Since for (a, b, c, d)
since c= a+b
and d = b+c
then d-b = (b+c) - b = c
By Binet's formula, the nth Fibonacci number is approximately sqrt(5)*phi**n, where phi is the golden ration. You can use base phi logarithms to recover the index easily:
from math import log, sqrt
def fibs(n):
nums = [1,1]
for i in range(n-2):
nums.append(sum(nums[-2:]))
return nums
phi = (1 + sqrt(5))/2
def fibIndex(f):
return round((log(sqrt(5)*f,phi)))
To test this:
for f in fibs(20): print(fibIndex(f),f)
Output:
2 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
11 89
12 144
13 233
14 377
15 610
16 987
17 1597
18 2584
19 4181
20 6765
Of course,
def adjacentFibs(f,g):
return abs(fibIndex(f) - fibIndex(g)) == 1
This fails with 1,1 -- but there is little point for explicit testing special logic for such an edge-case. Add it in if you want.
At some stage, floating-point round-off error will become an issue. For that, you would need to replace math.log by an integer log algorithm (e.g. one which involves binary search).
On Edit:
I concentrated on the question of how to recover the index (and I will keep the answer since that is an interesting problem in its own right), but as #LeandroCaniglia points out in their excellent comment, this is overkill if all you want to do is check if two Fibonacci numbers are adjacent, since another consequence of Binet's formula is that sufficiently large adjacent Fibonacci numbers have a ratio which differs from phi by a negligible amount. You could do something like:
def adjFibs(f,g):
f,g = min(f,g), max(f,g)
if g <= 34:
return adjacentFibs(f,g)
else:
return abs(g/f - phi) < 0.01
This assumes that they are indeed Fibonacci numbers. The index-based approach can be used to verify that they are (calculate the index and then use the full-fledged Binet's formula with that index).

Possible Combination that could be made with the same number

I'm currently working on a C program that will calculate the number of possibilities that could be made with the same number. For example: 444 should produce 6 number of possibilities (it counts as 4,4,4,44,44,444). I currently think to use a for loop with if statement in it to solve the problem. Thank you
It's unclear what you are asking, but if I understand correctly there is a very simple formula for that
Given a number of n caracteres, you will have:
1 subset of size n
2 subsets of size n-1
...
n subset of size 1
So you re computing the sum of i for i in 1..n which is
n*(n+1)/2
In your exemple 444 is of size 3, and 3*(3+1)/2 = 6

Minimum Number of Operations to make an array sorted

I have been trying this problem on spoj, not able to come up with the correct approach.
What is the correct algo to solve the problem ?
You should find longest consecutive increasing subsequence, which can be done in O(n log n) (by sorting array), after that, the number of changes needed is N - longest consecutive increasing subsequence. Note that by consecutive I mean there order in sorted array.
e.g:
1 7 6 2 5 4 3 => 1-2-3 is longest consecutive increasing subsequence,
number of moves needed is 4.
1 6 4 3 5 2 7 => 1-2 or 4-5 or 6-7 is longest consecutive increasing
subsequence, note that 1-4-5-7 is longest increasing subsequence but
number of moves needed is 5 not 3.
Why this works:
Best algorithm does not changes some of a items places, call biggest subsequence without changes as X, you wont change the position of X items related to each other during operations, so they should be sorted in increasing mode. But because you just allowed to move some items in the first or the last of array, you can't put any item between X items (note that we assumed X is biggest unchanged subsequence during operations), So there should be no gap between X items. so they should be sorted and consecutive in sorted format.
So number of changes needed couldn't be smaller than the N- length of X, but also is not hard to do your job with N-length of X operation.

Resources