Project Euler #167: Investigating Ulam sequences

Sort by

recency

|

9 Discussions

|

  • + 1 comment

    Hey! I've done this in c. Some of the test cases are accepted but the others show segmentation fault. Any suggestions?

    #include<stdio.h>
    int main()
    {
    	int i,n,k,num;
    	scanf("%d %d",&n,&k);
    	int arr[k];
    	arr[0]=2;
    	arr[1]=(2*n)+1;
    	num=arr[0]+arr[1];
    	for(i=2;i<k;num++)
    	{
    		if(sumcheck(num,arr,i))
    		{
    			arr[i]=num;
    			i++;
    		}
    	}
    	printf("%d",arr[k-1]);
    	return 0;
    }
    int sumcheck(int num,int arr[],int size)
    {
    	int i,j,k=0;
    	for(i=0;i<size;i++)
    	{
    		for(j=0;j<size;j++)
    		{
    			if(arr[j]+arr[i]==num)
    			{
    				k++;
    			}
    		}
    	}
    	if(k==2)
    	{
    		return 1;
    	}
    	else
    	{
    		return 0;
    	}
    }
    
  • + 0 comments

    public class Solution {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
     try{
    
     int n = in.nextInt(); 
     int k = in.nextInt(); 
    
     int array[] = new int[k+1]; 
    
        array[0]=2; 
       array[1]=2*n+1; 
        List<Integer> list2 = new ArrayList<Integer>();
       for(int i = 2; i<k; i++){
            List<Integer> list = new ArrayList<Integer>();
    
           for(int j = 0; j<i-1; j++){
               for(int kk=j+1; kk<i; kk++){
                   int a = array[j]+array[kk]; 
                   if(a>array[i-1]){list.add(a);}; 
               }
           }
          Collections.sort(list);
                // System.out.println("list 1"+ list );
          Set<Integer> set = new HashSet<Integer>(list);
    
      for(Integer tmp:set)
         {       
          if(Collections.frequency(list, tmp)==1){
                    list2.add(tmp);       
          }
    }
    
         Collections.sort(list2); 
           array[i]=list2.get(0); 
          //System.out.println("list 1" +list2 );
           list2.clear(); 
       }
      System.out.println(array[k-1] );
    
    
    
    }catch(Exception e){
        System.out.println("Error"); 
    }
    }      
    

    }

  • + 0 comments
    def check(li,c):
        ii=0
        flag=1
        smallestval=c
        for i in li:
            for j in li[ii:]:
                if i==j:
                    pass
                else:
                    val=int(i)+int(j)
    
                    if smallestval!=val and val>c:
                      if (val<smallestval and smallestval!=c):
                            smallestval=val
                    if smallestval==c:
                        if val>smallestval:
                              smallestval=val
    
            ii+=1
        f=0
        ii=0
        for i in li:
            for j in li[ii:]:
                if int(i)+int(j)==smallestval:
                    f+=1
    
            ii+=1
    
        if f==1:
            return smallestval
        return c
    k = 1
    v1, k = raw_input("enter two numbers").split()
    if int(v1)>=2 and int(v1)<=10:
        if int(k)>=1 and int(k)<=10**11:
            v2 = 2 * int(v1) + 1
            li = []
            li.append(v1)
            li.append(v2)
            li.append(int(v1) + int(v2))
            c=int(v1)+int(v2)
            while len(li) != int(k):
    
                copy=c
                c = check(li,c)
                if copy==c:
                    c+=1
                    continue
                li.append(c)
            print li[int(k)-1]
    

    it takes a bit long time, can anyone please let me know a better way to do it.. it worked for the first test case though.

  • + 1 comment

    import java.util.; import java.text.; import java.math.; import java.util.regex.;

    public class Solution {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
     int n = in.nextInt(); 
     int k = in.nextInt(); 
    
     int array[] = new int[k+2]; 
       array[0]=n; 
       array[1]=2*n+1; 
    
        if(k>2){
    List<Integer> list2 = new ArrayList<Integer>();
       for(int i = 2; i<=k; i++){
            List<Integer> list = new ArrayList<Integer>();
    
           for(int j = 0; j<i-1; j++){
               for(int kk=j+1; kk<i; kk++){
                   int a = array[j]+array[kk]; 
                   if(a>array[i-1]){list.add(a);}; 
               }
           }
          Collections.sort(list);
          Set<Integer> set = new HashSet<Integer>(list);
    
      for(Integer tmp:set)
         {       
          if(Collections.frequency(list, tmp)==1){
                    list2.add(tmp);       
          }
    }
    
         Collections.sort(list2); 
           array[i]=list2.get(0); 
    
           list2.clear(); 
       }
        }
      System.out.println(array[k-1] );
    
    }} 
    
  • + 0 comments

    Can anyone please suggest a optimal logic. Mine is giving a TLE. Thanks in advance.'