﻿ Java exercises: Find any number between 1 and n that can be expressed as the sum of two cubes in two (or more) different ways - w3resource

# Java Exercises: Find any number between 1 and n that can be expressed as the sum of two cubes in two (or more) different ways

## Java Numbers: Exercise-21 with Solution

Write java program to find any number between 1 and n that can be expressed as the sum of two cubes in two (or more) different ways.

//http://introcs.cs.princeton.edu/java/13flow/Ramanujan.java.html
Here are some examples of Ramanujan numbers :
1729 = 1^3 + 12^3 = 9^3 + 10^3
* 10000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
* 100000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
46683 = 3^3 + 36^3 = 27^3 + 30^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
40033 = 9^3 + 34^3 = 16^3 + 33^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
65728 = 12^3 + 40^3 = 31^3 + 33^3
64232 = 17^3 + 39^3 = 26^3 + 36^3

Sample Solution:

Java Code:

``````import java.util.Scanner;
public class Example21  {

public static void main(String[] args) {

int n = 100000;
// for each a, b, c, d, check whether a^3 + b^3 = c^3 + d^3
for (int a = 1; a <= n; a++) {
int a3 = a*a*a;
if (a3 > n) break;

for (int b = a; b <= n; b++) {
int b3 = b*b*b;
if (a3 + b3 > n) break;

for (int c = a + 1; c <= n; c++) {
int c3 = c*c*c;
if (c3 > a3 + b3) break;

for (int d = c; d <= n; d++) {
int d3 = d*d*d;
if (c3 + d3 > a3 + b3) break;

if (c3 + d3 == a3 + b3) {
System.out.print((a3+b3) + " = ");
System.out.print(a + "^3 + " + b + "^3 = ");
System.out.print(c + "^3 + " + d + "^3");
System.out.println();
}
}
}
}
}
}
}
```
```

Sample Output:

```1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
46683 = 3^3 + 36^3 = 27^3 + 30^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
40033 = 9^3 + 34^3 = 16^3 + 33^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
65728 = 12^3 + 40^3 = 31^3 + 33^3
64232 = 17^3 + 39^3 = 26^3 + 36^3
```

Flowchart: Java Code Editor:
{"ft_lang_label":"__label__en","ft_lang_prob":0.5620019,"math_prob":1.0000094,"size":2598,"snap":"2019-51-2020-05","text_gpt3_token_len":1075,"char_repetition_ratio":0.13685428,"word_repetition_ratio":0.41860464,"special_character_ratio":0.5338722,"punctuation_ratio":0.10398614,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":1.0000099,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-09T10:46:54Z\",\"WARC-Record-ID\":\"<urn:uuid:45990618-f7b8-4928-a4fc-11d7479a8559>\",\"Content-Length\":\"113099\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2f82060c-23ec-468b-93b0-d0df618925e9>\",\"WARC-Concurrent-To\":\"<urn:uuid:6df7a3f6-d390-4035-9fa7-586dcda583fc>\",\"WARC-IP-Address\":\"104.26.15.93\",\"WARC-Target-URI\":\"https://www.w3resource.com/java-exercises/numbers/java-number-exercise-21.php\",\"WARC-Payload-Digest\":\"sha1:ZBJ46OPQEOD4OWDXUJSR2G3A6OH5R2GU\",\"WARC-Block-Digest\":\"sha1:VWAQ4LFAFECEZHPUMPXC5ELLCT5LB5ZP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540518627.72_warc_CC-MAIN-20191209093227-20191209121227-00476.warc.gz\"}"}