The algorithm of julia:the power sum _julia

Source: Internet
Author: User

There is an algorithm problem in Hackerrank,

That is, a number (input) can be made by a sequence [1 2 3 4 ...] The power level (the value of 3 is 2 o'clock, that is, the square, the cubic, ...) is added. How many possibilities are there, please?

Specifically:

When: input =10;p ower = 2;
There are 10 =1^2 +3^2; there is a way to build it.
When: input =100;power = 2;
Have 100=10^2
= 6^2+8^2
=1^2+3^2+5^2+7^2 three kinds of construction methods.

That is, when input (<=1000), Power (>=2) to determine the number of combinations.

First, construct a coefficient matrix (0,1)
In the selected sequence, such as when power=2, sequence [1,4,9,16,25,36,49,64,81,100] ordinal is the key, could have been a trial, but put in a matrix, it will be more convenient.

The task below is to construct an exhaustive (0,1) matrix with max_power_n entries, i.e. the values obtained from the input power root (input=100, power=2,max_power_n =sqrt (100) =10).

The idea is to build a (0,1) matrix that can include various situations from the mat =[1 0;1 1; 0 1; 0 0]

Where: The number of columns is: Max_power_n. Number of lines.
Number of rows: 2^ (max_power_n). Because each column is equivalent to a variable, its value is only 2 possible, 0 or 1, and that's all that might be.

function Get_matrix (n::int64)
   data =[];
   If n<2
      error ("n<1!");
   Else
      Mat =[1 0;1 1; 0 1; 0 0];
      For i=2:n-1
          Temp1 =hcat (Mat,zeros (MAT) [:, 1]);
          Temp2= Hcat (Mat,ones (MAT) [:, 1]);
          Data =[TEMP1;TEMP2];
          Mat =copy (data);
       End return
          data
    end
 

Second, output function

The following function can get the overall result:

function Get_result (input,power)
   max_power_n =int64 (ceil (input^));
   Out_matrix =get_matrix (max_power_n);
   Sum_matrix =out_matrix* (Collect (1:max_power_n). ^power);
   Output =sum (sum_matrix.==input);
   println ("Input: $input power: $power =>result: $output");
   return output;
End

Output

Julia> Get_result (100,2)
input:100 power:2 =>result:3
3

julia> get_result (10,2)
input:10 Power:2 =>result:1
1

julia> get_result (10,3)
input:10 power:3 =>result:0
0

julia> Get_result (100,4)
input:100 power:4 =>result:0
0

julia> get_result (120,2)
input:120 Power : 2 =>result:4

But how do you see the composition of the detail?

Third, how to output details of the results.

Now that you want to output the details, let's do some optimizations:

function Get_result_detail (input,power)
   max_power_n =int64 (ceil (input^));
   Out_matrix =get_matrix (max_power_n);
   Series =collect (1:max_power_n). ^power;
   Sum_matrix =out_matrix*series;
   VEC =sum_matrix.==input;
   Println_detail (out_matrix,series,vec,input,power);
End

Let's write a function that outputs a detail result:

function Println_detail (out_matrix,series,vec,input,power)
    row,col=size (Out_matrix)
    n =0;
    For i =1:row
       if vec[i]==true
          is_print_row =false;
          n =n+1;
          For J=1:col
             mat = out_matrix[i,j];
             Ser = series[j];
             Is_print_row ==false && print ("$n th: =>");
             Is_print_row =true;
             If Mat==1 
                 print ("$ser");
             End
          -end print ("\ n");
       End
    -
    println ("Input: $input power: $power => Total Result: $n");
End

Iv. Results output of optimization

Output_detail =>

Julia> Get_result_detail (100,2)
1st: => 1  9
2nd: =>
3rd: = >
input:100 power:2 => total result:3

julia> get_result_detail (102,2)
1st: => 1  16< c12/>36
2nd: => 4  9
3rd: => 1  4  Bayi
input:102 Power:2 => Total Result:3

julia> get_result_detail (225,2)
1st: => 9
2nd: =>
3rd: => 1  4  9 (  121)
4th: => 4  121
5th: => 4  121
6th: => 4  25< c50/>36  144
7th: => 1  144
8th: => bayi  144
9th: => 4  16< c60/>36  169
TH: => 4  9  196
th: => 4  196
th: => 225
input:225 power:2 => Total Result:12

This detail can also be seen clearly.

There is a question as to why power cannot be 1. I think if the power is 1, if the input is larger, you imagine how large the matrix would be, leading to a direct memory overflow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.