[Challenge] Cube of Numbers


#1

Write a program(in any language you prefer) to display the cube of numbers in the following pattern:

1^3 = 1
2^3 = 3 + 5
3^3 = 7 + 9 + 11
4^3 = 13 + 15 + 17 + 19
5^3 = 21 + 23 + 25 + 27 + 29
...and so on

You can set the limit of displaying the pattern as per your convenience.


#2

Hi, here is my solution in JavaScript.

function cube (n) {
    const square = n*n;
    const step = 2;
    const start = n - 1;
    var arr = [];
    for (var i = 0 ; i < n ; i++) {
       	arr.push(square - start + step*i);
    }
    return "" + n + "^3 = " + arr.join(' + ');
}

function printCubes (n) {
    for (var i = 1 ; i < n ; i++) {
        document.body.innerHTML += '<p><code>' + cube(i) + '</code></p>';
    }
}

printCubes(10);

https://jsfiddle.net/kxdaz3ok/6/


#3

Here is my submission in Python 3:

k = 1
for i in range(1, 23):
    print("%d^3" % i, end='')
    for j in range(i):
        if (j == 0):
            print(" = %d" % k, end='')
        else:
            print(" + %d" % k, end='')
        k += 2
       
    print("")

I had to learn how to suppress newlines.
Here is the output:

1^3 = 1
2^3 = 3 + 5
3^3 = 7 + 9 + 11
4^3 = 13 + 15 + 17 + 19
5^3 = 21 + 23 + 25 + 27 + 29
6^3 = 31 + 33 + 35 + 37 + 39 + 41
7^3 = 43 + 45 + 47 + 49 + 51 + 53 + 55
8^3 = 57 + 59 + 61 + 63 + 65 + 67 + 69 + 71
9^3 = 73 + 75 + 77 + 79 + 81 + 83 + 85 + 87 + 89
10^3 = 91 + 93 + 95 + 97 + 99 + 101 + 103 + 105 + 107 + 109
11^3 = 111 + 113 + 115 + 117 + 119 + 121 + 123 + 125 + 127 + 129 + 131
12^3 = 133 + 135 + 137 + 139 + 141 + 143 + 145 + 147 + 149 + 151 + 153 + 155
13^3 = 157 + 159 + 161 + 163 + 165 + 167 + 169 + 171 + 173 + 175 + 177 + 179 + 181
14^3 = 183 + 185 + 187 + 189 + 191 + 193 + 195 + 197 + 199 + 201 + 203 + 205 + 207 + 209
15^3 = 211 + 213 + 215 + 217 + 219 + 221 + 223 + 225 + 227 + 229 + 231 + 233 + 235 + 237 + 239
16^3 = 241 + 243 + 245 + 247 + 249 + 251 + 253 + 255 + 257 + 259 + 261 + 263 + 265 + 267 + 269 + 271
17^3 = 273 + 275 + 277 + 279 + 281 + 283 + 285 + 287 + 289 + 291 + 293 + 295 + 297 + 299 + 301 + 303 + 305
18^3 = 307 + 309 + 311 + 313 + 315 + 317 + 319 + 321 + 323 + 325 + 327 + 329 + 331 + 333 + 335 + 337 + 339 + 341
19^3 = 343 + 345 + 347 + 349 + 351 + 353 + 355 + 357 + 359 + 361 + 363 + 365 + 367 + 369 + 371 + 373 + 375 + 377 + 379
20^3 = 381 + 383 + 385 + 387 + 389 + 391 + 393 + 395 + 397 + 399 + 401 + 403 + 405 + 407 + 409 + 411 + 413 + 415 + 417 + 419
21^3 = 421 + 423 + 425 + 427 + 429 + 431 + 433 + 435 + 437 + 439 + 441 + 443 + 445 + 447 + 449 + 451 + 453 + 455 + 457 + 459 + 461
22^3 = 463 + 465 + 467 + 469 + 471 + 473 + 475 + 477 + 479 + 481 + 483 + 485 + 487 + 489 + 491 + 493 + 495 + 497 + 499 + 501 + 503 + 505

#7

finally figured out how to keep the indentation.

current_num = 1
for x in range(1, 15):
    print x, "^", "3", ' = ' ,
    for y in range(x-1):
        print current_num, " ", 
        current_num += 2
    print current_num 
    current_num += 2

#8

You are almost there......but the output should display the cube of the number as the sum of that many consecutive odd numbers. For example, the cube of 3 is 27, which is the sum of 3 numbers, i.e. 7 + 9 + 11; similarly the cube of 4 is 64, which is the sum of 4 numbers, i.e. 13 + 15 + 17 + 19.....and so on.
The output should look the same as mentioned in the question.


#9

Python 3 code with cmin and cmax to print a range of cubes:

cmin=11
cmax=14
for i in range(cmin,cmax+1):
    print("%a^3 = " % i, end="")
    print(" + ".join(str(i*i - i + 1 + j*2) for j in range(i)))

Output:

11^3 = 111 + 113 + 115 + 117 + 119 + 121 + 123 + 125 + 127 + 129 + 131
12^3 = 133 + 135 + 137 + 139 + 141 + 143 + 145 + 147 + 149 + 151 + 153 + 155
13^3 = 157 + 159 + 161 + 163 + 165 + 167 + 169 + 171 + 173 + 175 + 177 + 179 + 181
14^3 = 183 + 185 + 187 + 189 + 191 + 193 + 195 + 197 + 199 + 201 + 203 + 205 + 207 + 209

The sequence produced by the second print was initially a rather ugly loop that I condensed using list comprehension first, then learned how to print on the fly without storing the list. I've built the algorithm after observing that:

1^3 = 1-0
2^3 = 4-1 + ...
3^3 = 9-2 + ...
4^3 = 16-3 + ...
5^3 = 25-4 + ...
etc.

Feels like it could be an oneliner, but haven't figured out how to do it yet, and not sure if that's pythonic either.


#10

Here's my attempt in Python 3. I kept the upper limit small for demonstration purposes.

For quick reference, this outputs:

1^3 = 1
2^3 = 3 + 5
3^3 = 7 + 9 + 11
4^3 = 13 + 15 + 17 + 19
5^3 = 21 + 23 + 25 + 27 + 29
6^3 = 31 + 33 + 35 + 37 + 39 + 41
7^3 = 43 + 45 + 47 + 49 + 51 + 53 + 55
8^3 = 57 + 59 + 61 + 63 + 65 + 67 + 69 + 71
9^3 = 73 + 75 + 77 + 79 + 81 + 83 + 85 + 87 + 89


#11

I think the output does look the same:

1 ^ 3  =  1
2 ^ 3  =  3   5
3 ^ 3  =  7   9   11
4 ^ 3  =  13   15   17   19
5 ^ 3  =  21   23   25   27   29
6 ^ 3  =  31   33   35   37   39   41
7 ^ 3  =  43   45   47   49   51   53   55
8 ^ 3  =  57   59   61   63   65   67   69   71
9 ^ 3  =  73   75   77   79   81   83   85   87   89
10 ^ 3  =  91   93   95   97   99   101   103   105   107   109
11 ^ 3  =  111   113   115   117   119   121   123   125   127   129   131
12 ^ 3  =  133   135   137   139   141   143   145   147   149   151   153   155
13 ^ 3  =  157   159   161   163   165   167   169   171   173   175   177   179   181
14 ^ 3  =  183   185   187   189   191   193   195   197   199   201   203   205   207   209
None

#12

oops forgot to put the + signs


#13

it's all right....:smiley:
happy coding!!!


#14

My solution in Java

class Cubes{
    public static void main(String[]args){
        for(int i=1; i<=5; i++){
            int a=i*(i-1)+1;
            System.out.print(i+"^3 = "+a);
            a+=2;
            for(int j=1; j<=i-1; j++){
                System.out.print(" + "+a);
                a+=2;
            }
            System.out.println();
        }
    }
}

#15

My solution in c++:

#include <iostream>


using namespace std;

int main(){
    int bCube = 1;
    int additiveFactors = 1;
    int num;
    int limit;

    cout << "Please enter the max num: ";
    cin >> limit;

    for (bCube; bCube <= limit; bCube++){
            cout << bCube << "^3 = ";
        for (int i = 0; i < bCube; i++){
            num = additiveFactors+2*i;
            cout << num;
            if (i < bCube-1){
                cout << " + ";
            }
        }
        additiveFactors = num+2;
        cout << endl;
    }
}

#16

Here we go (Python 2.7):

from __future__ import print_function
x = input("Up to what integer shall we go? ")
odds = [i for i in range(1, x ** 3) if i % 2 == 1]
z = x ** 3
for i in range(1,x+1):
    firstindex = sum((range(i)))
    the_long_list = (odds[firstindex:(firstindex+i)])
    cube = sum(the_long_list)
    print("%d^3 = " %(i) + " + ".join([str(x) for x in the_long_list]) + " = %d" %(cube))

#17

Java:

public static void displayCube(int n){
        int index=1;
        for (int i=1; i<=n; i++){
            String s=i+"^3=";
            long m=(long)(Math.pow(i,3));
            while (m>0){
                m-=index;
                s+=index+"+";
                index+=2;
                
            }
            s=s.substring(0,s.length()-1);
            System.out.println(s);
        }

#18

Discovered some patterns in the numbers :slight_smile:


#19

the crazy one-liner


#20

I didn't look at the answers before solving it myself, but I did forgot to print the results of several input values at the same time, so I made a change and wrapped everything into a loop.
It's in javascript:


#21

In perl a nice one liner - for those who aren't perl users shift pulls in this context takes the first element of @ARGV or the parameters... [ and perl -E executes the script turning on all the "cutting edge" features in this case say.

perl -E 'for$n(1..shift){say"$n^3 = ",join" + ",map{$n*$n-$n+$_*2-1}1..$n}' 20


#22

Sorry for bumping but In groovy you can also do it in one line quite well.

(1..10).each{println "${it}^3 = ${(it*it-it+1..it*it+it).step(2).join(" + ")}"}

(1…10) is the amount of times you want to loop, here it does 10 loops.
For the part after the equals I just get every number from the smallest one to the biggest one and print them all with a step of 2.

Groovy is not on repl.it but you can try it on the groovy console by viewing the output tab.
https://groovyconsole.appspot.com/