-
Re: 500 Ways to Print 1 to 10
Pretty sure I didn't see a solution that uses a stack.
23. Java
Code :
public class OneToTen
{
public static void main(String[] args)
{
Stack stack = new Stack();
stack.push(10);
stack.push(9);
stack.push(8);
stack.push(7);
stack.push(6);
stack.push(5);
stack.push(4);
stack.push(3);
stack.push(2);
stack.push(1);
while (stack.size() > 0)
{
System.out.println(stack.pop() + " ");
}
}
}
public class Stack
{
ArrayList<Integer> data;
public Stack()
{
data = new ArrayList<Integer>();
}
public void push(int num)
{
data.add(num);
}
public int pop()
{
return data.remove(data.size() - 1);
}
public int size()
{
return data.size();
}
}
-
Re: 500 Ways to Print 1 to 10
24. java
Code :
public static void main(String[] args){
String crazy = ":crazy nuts";
System.out.println(crazy.indexOf("c"));
System.out.println(crazy.indexOf("r"));
System.out.println(crazy.indexOf("a"));
System.out.println(crazy.indexOf("z"));
System.out.println(crazy.indexOf("y"));
System.out.println(crazy.indexOf(" "));
System.out.println(crazy.indexOf("n"));
System.out.println(crazy.indexOf("u"));
System.out.println(crazy.indexOf("t"));
System.out.println(crazy.indexOf("s"));
}
-
Re: 500 Ways to Print 1 to 10
25. Java
Code :
public class OneToTen
{
public static void main(String[] args)
{
Pattern pat = Pattern
.compile("([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])");
Matcher mat = pat.matcher("1234567890");
if (mat.find())
{
System.out.print(mat.group(1) + " ");
System.out.print(mat.group(2) + " ");
System.out.print(mat.group(3) + " ");
System.out.print(mat.group(4) + " ");
System.out.print(mat.group(5) + " ");
System.out.print(mat.group(6) + " ");
System.out.print(mat.group(7) + " ");
System.out.print(mat.group(8) + " ");
System.out.print(mat.group(9) + " ");
System.out.print(mat.group(1));
System.out.print(mat.group(10));
}
}
}
-
Re: 500 Ways to Print 1 to 10
18. C++ (fixed (; )
Code :
#include <iostream>
int main()
{
int* a = new int[10];
for (int i = 0; i < 10; i++)
{
*a = 10 - i;
a++;
}
for (int i = 0; i < 10; i++)
{
cout << a* << " ";
a--;
}
delete[] a;
return 0;
}
-
Re: 500 Ways to Print 1 to 10
26. PHP :D
Code :
<?php
for($i=0;$i<=10;$i++)
{
echo"$i";
}
?>
thx to statement_missing
-
Re: 500 Ways to Print 1 to 10
27. pascal
Code :
uses wincrt;
var i : integer;
begin
i := 1;
while i <= 10 do print(i)
end.
thx to vrainz :D
-
Re: 500 Ways to Print 1 to 10
28. Java
Code :
import java.util.Arrays;
public class OneToTen {
public static void main(String... arguments) {
System.out.println(Arrays.toString(arguments));
}
}
Execute it using something like
Quote:
java -cp . OneToTen 1 2 3 4 5 6 7 8 9 10
// Json
-
Re: 500 Ways to Print 1 to 10
29. VB6
Code :
Private Sub Form_Load()
AutoRedraw = True
Print "Print number 1 - 10"
For i = 1 To 10
Print i
Next
End Sub
http://i199.photobucket.com/albums/aa63/vrainz/vb6.png
again thx to vrainz http://www.indowebster.web.id/images...ovea/boong.gif
-
Re: 500 Ways to Print 1 to 10
30.VB.Net
credit to vrainz
Code :
Public Class Form1
Private b1 As Bitmap
Private g1 As Graphics
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles
MyBase.Activated
Static done As Boolean = False
If Not done Then
Size = New Size(300, 350)
Text = "Print 1 - 10"
b1 = New Bitmap(Width, Height, Me.CreateGraphics())
g1 = Graphics.FromImage(b1)
done = True
FormPrint("Print number from 1 - 10", 25, 10)
FormPrint("1", 25, 25)
FormPrint("2", 25, 40)
FormPrint("3", 25, 55)
FormPrint("4", 25, 70)
FormPrint("5", 25, 85)
FormPrint("6", 25, 100)
FormPrint("7", 25, 115)
FormPrint("8", 25, 130)
FormPrint("9", 25, 145)
FormPrint("10", 25, 160)
End If
End Sub
Private Sub FormPrint(ByVal t$, ByVal x1%, ByVal y1%)
g1.DrawString(t, New Font("Comic Sans MS", 12), Brushes.Chocolate, x1, y1)
Me.CreateGraphics.DrawImage(b1, 0, 0)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles
MyBase.Paint
e.Graphics.DrawImage(b1, 0, 0)
End Sub
End Class
http://i199.photobucket.com/albums/a...ainz/vbnet.png
-
Re: 500 Ways to Print 1 to 10
31. VFP (actually any XBase) db
-
Re: 500 Ways to Print 1 to 10
idk if this has been done or not
32. Java
Code :
for ( int i = 9; i > 0 ; i-- ) {
system.out.println(10 - i);
}
-
Re: 500 Ways to Print 1 to 10
#33 PHP
Code :
<?php
echo "1 2 3 4 5 6 7 8 9 10";
?>
-
Re: 500 Ways to Print 1 to 10
LOL. This is getting a bit crazy..
#34 Java
Code :
import java.io.*;
import java.text.*;
public class Print1to10 {
public static long size;
public static double ans;
public static void main(String[] args) throws Exception {
Writer output = null;
File file = new File("file.txt");
output = new BufferedWriter(new FileWriter(file));
output.write("JavaProgrammingForums.com");
output.close();
getSize(file);
}
public static void getSize(File file) {
size = file.length();
maths();
}
public static void maths() {
double myDbl = 493.8;
ans = (myDbl * size);
DecimalFormat df = new DecimalFormat("#");
System.out.print(df.format(ans));
partTwo();
}
public static void partTwo(){
double ans2 = (1357.82 * 500);
DecimalFormat df2 = new DecimalFormat("#");
System.out.print(df2.format(ans2));
}
}
Output:
-
Re: 500 Ways to Print 1 to 10
I’m not sure if there have been any C# entries yet, this has to be one of the longest methods yet as well!! :(
35 C#
Code :
protected void Page_Load(object sender, EventArgs e)
{
string fileName = @"C:\Count.xml";
CountToTenAndSerialize(fileName);
CountToTenAndDeserialize(fileName);
}
public class Count
{
public int count;
}
private void CountToTenAndSerialize(string fileName)
{
List<Count> _countList = new List<Count>();
int counter = 0;
while(counter <= 10)
{
Count countList = new Count();
countList.count = counter;
_countList.Add(countList);
counter++;
}
XmlSerializer serializer = new XmlSerializer(typeof(List<Count>));
TextWriter tr = new StreamWriter(fileName);
serializer.Serialize(tr, _countList);
tr.Close();
}
private void CountToTenAndDeserialize(string fileName)
{
TextReader tr = new StreamReader(fileName);
XmlRootAttribute rootAttribute = new XmlRootAttribute("ArrayOfCount");
XmlSerializer deserializer = new XmlSerializer((typeof(List<Count>)), rootAttribute);
List<Count> _countList = (List<Count>)deserializer.Deserialize(XmlReader.Create(tr));
tr.Close();
foreach (Count count in _countList)
{
Response.Write(count.count.ToString() + "<br />");
}
}
-
Re: 500 Ways to Print 1 to 10
Quote:
Originally Posted by
antony_t
I’m not sure if there have been any C# entries yet, this has to be one of the longest methods yet as well!! :(
Nope, solutions 16 and 17 are longer ;) Basically they're solving a ten dimensional linear algebra problem problem who's solution is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Some more fun with math:
36. Java
Code :
public class OneToTen
{
public static void main(String[] args)
{
int num = 1;
for (int j = 0; j < 10; j++)
{
int carry = 0;
System.out.print(num + " ");
carry = num & 0x1;
num = num ^ 0x1;
for (int i = 1; i < 8; i++)
{
int nextCarry = num & (carry << 1);
num = num ^ (carry << 1);
carry = nextCarry;
}
}
}
}
-
Re: 500 Ways to Print 1 to 10
#37 Java
Code :
public class Print1to10 {
public static void main(String[] args) {
for (int a = 0; a < 11; a++) {
switch (a) {
case 1:
System.out.print("1 ");
break;
case 2:
System.out.print("2 ");
break;
case 3:
System.out.print("3 ");
break;
case 4:
System.out.print("4 ");
break;
case 5:
System.out.print("5 ");
break;
case 6:
System.out.print("6 ");
break;
case 7:
System.out.print("7 ");
break;
case 8:
System.out.print("8 ");
break;
case 9:
System.out.print("9 ");
break;
case 10:
System.out.print("10 ");
break;
}
}
}
}
-
Re: 500 Ways to Print 1 to 10
#38 Shell Script
Code :
for a in {1..10}
do
echo -n "$a "
done
-
Re: 500 Ways to Print 1 to 10
Hmm.. We are still a long way off 500 ways people! lol
-
Re: 500 Ways to Print 1 to 10
#39
Code :
public static void main(String[] args) {
long time = System.currentTimeMillis();
long temp = 0;
while ( temp < 10 ){
long current = System.currentTimeMillis();
if ( (current-time)/100 != temp ){
temp = (current-time)/100;
System.out.println( temp );
}
}
}
-
Re: 500 Ways to Print 1 to 10
#40 Java
Code :
public static void main(String[] args) {
String tester = "", fullString = "Count down";
while(tester.length() < fullString.length()){
System.out.println((tester.length() + 1));
tester = tester + fullString.charAt(tester.length());
}
}
-
Re: 500 Ways to Print 1 to 10
#41 Java
Code :
import java.util.concurrent.*;
public class OneToTenThreads implements Runnable {
ThreadPoolExecutor executor;
public static void main(String[] args) {
new OneToTenThreads().start();
}
private void start() {
executor = new ThreadPoolExecutor(10, 10, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
while (executor.getActiveCount() < executor.getCorePoolSize()) {
executor.execute(this);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.exit(0);
}
@Override
public void run() {
System.out.println(executor.getActiveCount());
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
-
Re: 500 Ways to Print 1 to 10
#42 Java
Code :
public class OneToTenPrime {
public static void main(String[] args) {
new OneToTenPrime().countPrimes(4);
}
private void countPrimes(int count) {
int primesFound = 0;
int testNumber = 2;
primesFound = 0;
testNumber = 2;
while (primesFound <= count) {
System.out.println(testNumber - 1);
primesFound += isPrime(testNumber) ? 1 : 0;
testNumber++;
}
}
private boolean isPrime(int n) {
for (int i = 2; i <= (int) Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
-
Re: 500 Ways to Print 1 to 10
#43 Java - Converting from Base 33 (Triotrigesimal)!!!
Code :
public class SandBox {
public SandBox() {
String[] base33 = new String[]{"B","M","10","1B","1M","20","2B","2M","30","3B"};
for (String s : base33) {
int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;
if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else
limit = -Integer.MAX_VALUE;
multmin = limit / 33;
if (i < max) {
digit = Character.digit(s.charAt(i++), 33);
result = -digit;
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++), 33);
result *= 33;
result -= digit;
}
}
if (negative)
System.out.print(result/11+" ");
else
System.out.print(-result/11+" ");
}
}
public static void main(String[] args) {
new SandBox();
}
}
-
Re: 500 Ways to Print 1 to 10
ROFL
#44 Java - Brute forcing randomized SHA512 hashes!!
Code :
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
/**
* Test class for various things. Not relevent to anything
* @author LordQuackstar
*/
public class SandBox {
List<String> sha512hashes = new ArrayList(Arrays.asList(new String[]{
"bb90b23776dfde3333f63a924ebd2a039d80fc9280a7d1e9418529ced428930b69a95d55c4d9238f30b73789b4ebe0356bb9b8707025e3c527ca34825a160e2d",
"4774b6224b8e98b96b658092bee32c88c41b1a8c80dcfd7e1fdffc7be59c5f72eae3aecac37b0c7398154489066b0b022240a68daf4432849fabe75768faaf5e",
"3673a16a5983f5f5e04bf88d2c08e39631efe619726c5879d2d6907c00acb5d5689061b28cea52edab7c79dbfb450c961709c36c0d599b526c856e924f57e803",
"bb90b23776dfde3333f63a924ebd2a039d80fc9280a7d1e9418529ced428930b69a95d55c4d9238f30b73789b4ebe0356bb9b8707025e3c527ca34825a160e2d",
"3163a8d6a4540ecf1794ece0245f291154d30e1080359d2e994ef79c1a469aa0cd808769d9c7ee30ca342c6803d2ebcec3eb71a928d6db187dfb1fc2cf640395",
"84865a87593500aaaa29a49c382b84491eb97ac61a9264edd724aaaa81227040a557412b98841c14ed48b365f9a2f25faf7f59561d001bfa118070ec60dea8f3",
"08856a9022cc1f4b7c90b2d059e64acb6f6c5ac11da907d86db6a3072e9d821c59603c1ea94a2e537bea0a38320d678c482a66eaaf1a79c4d3432ea41e51b721",
"6ad275d26c200e81534d9996183c8748ddfabc7b0a011a90f46301626d709923474703cacab0ff8b67cd846b6cb55b23a39b03fbdfb5218eec3373cf7010a166",
"74c205daf6521128f2ad9009e44d9b608ea4940b5747ef6e74d616e4599ccaffcf12bb69ad38c8bbbfbd248b94fc8adddc3b091c7906cb05501dbea026e0d568",
"74a49c698dbd3c12e36b0b287447d833f74f3937ff132ebff7054baa18623c35a705bb18b82e2ac0384b5127db97016e63609f712bc90e3506cfbea97599f46f"}));
List<Integer> resultingNums = new ArrayList<Integer>();
public SandBox() {
for (String curHash : sha512hashes)
for (int num = 1; num <= 200; num++) {
MessageDigest md;
String message = Integer.toString(num);
try {
md = MessageDigest.getInstance("SHA-512");
md.update(message.getBytes());
byte[] mb = md.digest();
String out = "";
for (int i = 0; i < mb.length; i++) {
byte temp = mb[i];
String s = Integer.toHexString(new Byte(temp));
while (s.length() < 2)
s = "0" + s;
s = s.substring(s.length() - 2);
out += s;
}
if (out.equalsIgnoreCase(curHash)) {
resultingNums.add(num/11);
break;
}
} catch (NoSuchAlgorithmException e) {
System.out.println("ERROR: " + e.getMessage());
}
}
Integer[] ints = resultingNums.toArray(new Integer[0]);
Arrays.sort(ints);
System.out.println(StringUtils.join(ints," "));
}
public static void main(String[] args) {
new SandBox();
}
}
(this is getting out of hand
-
Re: 500 Ways to Print 1 to 10
#45
Code :
int i=40;
do
{
System.out.println((i%40+1));
i++;
}while(i<50);