-
Re: 500 Ways to Print 1 to 10
What's the fun if you arrive at the correct solution the first time :P
46. Java
Code :
public static void main(String[] args)
{
int[] numbers = new int[10];
boolean foundAnswer = false;
while(!foundAnswer)
{
for(int i = 0; i < numbers.length; i++)
{
numbers[i] =(int)( Math.random() * 10 + 1);
}
sort(numbers);
foundAnswer = true;
for(int i = 1; i < numbers.length && foundAnswer; i++)
{
if(numbers[i-1] == numbers[i])
{
foundAnswer = false;
}
}
}
System.out.println(numbers);
}
public static void sort(int[] numbers)
{
for (int i = 0; i < numbers.length; i++)
{
int min = 0;
for(int j = i; j < numbers.length; j++)
{
if(numbers[min] > numbers[j])
{
min = j;
}
}
int temp = numbers[min];
numbers[min] = numbers[i];
numbers[i] = temp;
}
}
-
Re: 500 Ways to Print 1 to 10
47. Java
More math...
Code :
int value = 2;
while(value <= 1024)
{
System.out.println(Math.log(value) / Math.log(2));
value <<= 1;
}
-
Re: 500 Ways to Print 1 to 10
48. Java
Code :
import java.util.Calendar;
public class Calendar1To10 {
public static void main(String[] args) {
Calendar start = Calendar.getInstance();
Calendar now = Calendar.getInstance();
while (now.get(Calendar.YEAR) < start.get(Calendar.YEAR) + 10) {
try {
Thread.sleep(1000 * 60 * 60 * 24);
Calendar temp = Calendar.getInstance();
if (temp.get(Calendar.YEAR) > now.get(Calendar.YEAR)) {
now = temp;
System.out.println(now.get(Calendar.YEAR) - start.get(Calendar.YEAR));
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
There still won't be 500 ways by the time this one completes executing ;)
-
Re: 500 Ways to Print 1 to 10
#49 Java
Code java:
public class Print1To10
{
public static void main(String[] args)
{
System.out.println(1);
System.out.println(1<<1);
System.out.println(1^1<<1);
System.out.println(1<<1<<1);
System.out.println(1^1<<1<<1);
System.out.println(1<<1^1<<1<<1);
System.out.println(1^1<<1^1<<1<<1);
System.out.println(1<<1<<1<<1);
System.out.println(1^1<<1<<1<<1);
System.out.println(1<<1^1<<1<<1<<1);
}
}
-
Re: 500 Ways to Print 1 to 10
#50. Logo 8-}
Code LOGO:
PENUP
LEFT 90
FORWARD 220
RIGHT 90
FORWARD 20
PENDOWN
RIGHT 45
FORWARD 12
RIGHT 135
FORWARD 30
PENUP
LEFT 90
FORWARD 50
LEFT 180
PENDOWN
FORWARD 20
RIGHT 90
FORWARD 20
RIGHT 90
FORWARD 20
LEFT 90
FORWARD 10
LEFT 90
FORWARD 20
PENUP
RIGHT 180
FORWARD 50
PENDOWN
FORWARD 20
RIGHT 90
FORWARD 30
RIGHT 90
FORWARD 20
PENUP
RIGHT 90
FORWARD 20
RIGHT 90
PENDOWN
FORWARD 20
PENUP
FORWARD 40
RIGHT 90
FORWARD 20
PENDOWN
LEFT 180
FORWARD 30
LEFT 135
FORWARD 25
LEFT 135
FORWARD 25
PENUP
FORWARD 40
LEFT 90
FORWARD 18
PENDOWN
LEFT 90
FORWARD 20
LEFT 90
FORWARD 10
LEFT 90
FORWARD 20
RIGHT 90
FORWARD 20
RIGHT 90
FORWARD 20
PENUP
RIGHT 90
FORWARD 30
RIGHT 90
FORWARD 70
PENDOWN
LEFT 180
FORWARD 20
LEFT 90
FORWARD 30
LEFT 90
FORWARD 20
LEFT 90
FORWARD 20
LEFT 90
FORWARD 20
PENUP
RIGHT 90
FORWARD 10
RIGHT 90
FORWARD 45
PENDOWN
FORWARD 20
RIGHT 120
FORWARD 34
PENUP
LEFT 120
FORWARD 45
PENDOWN
FORWARD 20
LEFT 90
FORWARD 30
LEFT 90
FORWARD 20
LEFT 90
FORWARD 30
PENUP
BACKWARD 20
PENDOWN
LEFT 90
FORWARD 20
PENUP
FORWARD 50
PENDOWN
LEFT 180
FORWARD 20
RIGHT 90
FORWARD 10
RIGHT 90
FORWARD 20
RIGHT 90
FORWARD 30
RIGHT 90
FORWARD 20
PENUP
RIGHT 180
FORWARD 45
LEFT 90
FORWARD 21
PENDOWN
RIGHT 45
FORWARD 12
RIGHT 135
FORWARD 30
PENUP
LEFT 90
FORWARD 8
PENDOWN
FORWARD 20
LEFT 90
FORWARD 30
LEFT 90
FORWARD 20
LEFT 90
FORWARD 30
You can copy-paste the source into the right editor screen over here.
-
Re: 500 Ways to Print 1 to 10
#51: There is always a chance that this takes forever to finish.
Code JAVA:
import java.util.TreeSet;
import java.util.Set;
public class OneToTen {
public static void main(String... args){
Set<Integer> numberSet = new TreeSet<Integer>();
int times = 0;
while(numberSet.size() < 10){
times++;
Integer i = (int)(Math.random()*10 +1);
numberSet.add(i);
}
for(int i : numberSet){
System.out.println(i);
}
//System.out.println("Times: " + times);
}
}
-
Re: 500 Ways to Print 1 to 10
#52. 8086 assembler using interrupts
Code Assembler:
org 100h
; set screen to text mode
mov ax, 3
int 10h
; init segment register
mov ax, 0b800h
mov ds, ax
; move characters' ASCII codes to video mem
mov [02h], 31h
mov [04h], 20h
mov [06h], 32h
mov [08h], 20h
mov [0ah], 33h
mov [0ch], 20h
mov [0eh], 34h
mov [10h], 20h
mov [12h], 35h
mov [14h], 20h
mov [16h], 36h
mov [18h], 20h
mov [1ah], 37h
mov [1ch], 20h
mov [1eh], 38h
mov [20h], 20h
mov [22h], 39h
mov [24h], 20h
mov [26h], 31h
mov [28h], 30h
; wait for keyb input
mov ah, 0
int 16h
ret
-
Re: 500 Ways to Print 1 to 10
#53 Using PHP's array_walk() and (very ugly) create_function():
PHP Code:
$nums = array(1,2,3,4,5,6,7,8,9,10);
array_walk($nums, create_function('$x', 'echo "$x ";'));
-
Re: 500 Ways to Print 1 to 10
#54. Arduino C:
Code Arduino C:
int counter = 1;
void setup()
{
Serial.begin(38400);
}
void loop()
{
Serial.println(counter);
++counter;
while(counter > 10)
{}
}
-
Re: 500 Ways to Print 1 to 10
#55
Code java:
public static void main(String[] args) {
long mask = 0xfl;
long value = 728121033505l;
int shifter = 0;
while(shifter < 40) {
System.out.println((value & mask) >> shifter);
mask = mask << 4;
shifter += 4;
}
}
It probably can be done with out shifter but my brain hurts.
-
Re: 500 Ways to Print 1 to 10
#56
Code java:
class OneToTen {
int[] ones = {1,1,1,1,1,1,1,1,1,1};
public static void main(String[] args) {
new OneToTen();
}
OneToTen() {
for(int index = 0; index < ones.length; index++) {
System.out.println(sum(index));
}
}
public int sum(int endValue) {
int total = 0;
for(int index = 0; index <= endValue; index++) {
total += ones[index];
}
return total;
}
}
-
Re: 500 Ways to Print 1 to 10
#57
Code java:
import java.util.ArrayList;
import java.util.Collections;
class OneToTenStrings {
ArrayList<String> words = new ArrayList<String>();
public static void main(String[] args) {
new OneToTenStrings();
}
OneToTenStrings() {
words.add("jaberwocky");
words.add("grazing");
words.add("cog");
words.add("inception");
words.add("frozen");
words.add("dark");
words.add("earnt");
words.add("a");
words.add("halftime");
words.add("be");
Collections.sort(words);
while(! words.isEmpty()) {
System.out.println(words.remove(0).length());
}
}
}
-
Re: 500 Ways to Print 1 to 10
Ok, I only read to page 4, so I hope no one posted this. Anyway, here is printing 1-10 using JavaScript from Java.
Code :
public class Main
{
public static void main(String[] args)
{
ScriptEngineManager man = new ScriptEngineManager();
ScriptEngine engine = man.getEngineByName( "rhino" );
try
{
engine.eval( "for (i = 1; i < 11; ++i){ print(i + \" \"); }" );
}
catch (ScriptException e)
{
System.err.println( "Muhahahaha, my attempt to look smart failed :P" );
}
}
}
-
Re: 500 Ways to Print 1 to 10
Here it goes (using do....while)
Code Java:
class One2Ten
{
public void display()
{
int a = 1;
do
{
System.out.println(a);a++;}
while(a<11);
}
}
-
Re: 500 Ways to Print 1 to 10
#60: Beanshell
Code :
print("1 2 3 4 5 6 7 8 9 10");
-
Re: 500 Ways to Print 1 to 10
-
Re: 500 Ways to Print 1 to 10
Code :
print(N) :- N < 11, write(N), N is N+1, print(N).
?-print(1).
A PROLOG solution.
-
Re: 500 Ways to Print 1 to 10
#63 Python
Code :
i = 1
while i <= 10:
print(i)
Think that's right, haven't done python in a while
-
Re: 500 Ways to Print 1 to 10
#64 Java
Code Java:
//package textField;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
* Prints one through ten with a JTextField
* @author TJStretch
*/
public class OneToTenTextfield extends JFrame
{
//Variables
/**
* The JTextField that 1 through 10 will be printed on
*/
private JTextField txtField;
/**
* Randomly generated serial version UID
*/
private static final long serialVersionUID = -5841505335372992754L;
//Constructors
public OneToTenTextfield()
{
setLayout(new BorderLayout());
setSize(125, 100);
setOneToTen();
add(txtField, BorderLayout.CENTER);
setVisible(true);
}
//Methods
/**
* Initialize and set txtField to be ready for use.
*/
private void setOneToTen()
{
if(txtField == null)
txtField = new JTextField();
String theStr = "";
for(int i = 1; i < 11; i++)
theStr += i+" ";
txtField.setText(theStr);
txtField.setEditable(false);
}
/**
* @param args
*/
public static void main(String[] args)
{
new OneToTenTextfield();
}
}
-
Re: 500 Ways to Print 1 to 10
65 C#:
Code C#:
delegate void count(count func, int i);
public static void doCount(count b, int i)
{
Console.Out.WriteLine(i);
if(i < 10)
b(b, i + 1);
}
static void Main(string[] args)
{
new count(doIt).Invoke(doIt, 1);
Console.In.ReadLine();
}
-
Re: 500 Ways to Print 1 to 10
#66 (I think---are we supposed to assign the numbers or is it done by some referee?): "seq" command
Oh, well...
Is this allowed?
For any system with seq utility (All UNIX systems that I have ever used had this. It is also in the GNU core utilities that are supplied with all Linux distros with which I am familiar.)
From a command line (or, you could put it in a shell script):
Cheers!
Z
-
Re: 500 Ways to Print 1 to 10
#67: Busybox
Similar to, but not exactly the same as the previous post (so I think it should be counted separately): For systems with busybox installed. (Many embedded Linux systems that don't have the full complement of GNU core utilities and even some desktop Linux systems have busybox.) On embedded systems, the seq command is probably a symbolic link to busybox, but it can be invoked explicitly as an argument to busybox, assuming its applet was compiled in, which it always is for my systems.)
From a command line or you could put it in a shell script):
Cheers!
Z
-
Re: 500 Ways to Print 1 to 10
#68: FORTRAN II
From the days when FORTRAN was a powerful acronym (not the sissified proper noun Fortran) and versions were designated with Roman Numerals (like world wars), here it is in FORTRAN II. Line printers in common use and the IBM 026 key punch machines used in Computer Centers of the day did not have lower case alphabetical characters, so it looks like we were always SHOUTING.
Code fortran:
C FORTRAN II FROM 1967
C
C FROM ZAPHOD_B IN 2012
C
WRITE(6,101) (I,I=1,10)
101 FORMAT(1X,I10)
END
Can still be compiled with GNU g77. I just executed it on my Linux workstation. It still works the way it did way back when. Or least that's what my Earth logbook from the 20th century indicates.
Back in the day. working on MainFrames, logical unit 6 was (almost) always the line printer, but could (usually) be changed by JCL (Job Control Language). Modern Fortran compiler installations on workstations (including GNU g77 on my Linux boxes) are, typically, set up to connect logical unit 6 to standard output (console window).
The almost magical (mystical, at least) formidable power of the FORTRAN FORMAT I/O interpreter still gasts my flabber after all these decades.
Cheers!
Z
-
Re: 500 Ways to Print 1 to 10
#69: Fortran 77
Fortran 77. Compile with GNU g77. (Also works with compilers for later versions, like Fortran 90, but I guess that wouldn't be allowed as a separate count)
Code fortran:
C Fortran 77
C
C Zaphod_b
C
Program PrintSequence
do i=1,10
print *, i
end do
Stop
End Program PrintSequence
Cheers!
Z
-
Re: 500 Ways to Print 1 to 10
#70: Fortran 90
Compile with GNU gfortran
Code fortran:
! Fortran 90 with a do loop
!
! Zaphod_b
!
Program PrintSequence
do i=1,10
write(*,'(1x,i0)') i
end do
Stop
End Program PrintSequence
Cheers!
Z